ContentText.contents 不可访问,因为它受保护级别限制
public class Operator
{
public Operator()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection ConnecionDB()
{
SqlConnection con = new SqlConnection("server=.;DataBase=ContentDB;Trusted_Connection=true;");
return con;
}
public static bool AddContent(ContentText ct)
{
try {
SqlConnection con = Operator.ConnecionDB();
con.Open();
string str = "insert into ContentText(userName,tel,email,contents)values(@userName,@tel,@email,@contents)";
SqlCommand cmd = new SqlCommand(str,con);
SqlParameter param = new SqlParameter("@userName",SqlDbType.Char,10);
param.Value=ct.userName;
cmd.Parameters.Add(param);
param = new SqlParameter("@email",SqlDbType.Char,20);
param.Value=ct.email;
cmd.Parameters.Add(param);
param = new SqlParameter("@tel",SqlDbType.Char,20);
param.Value = ct.tel;
cmd.Parameters.Add(param);
param = new SqlParameter("@contents",SqlDbType.NVarChar);
param.Value = ct.contents;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
}
都是public 怎么也不行 啊 求指导
------解决方案--------------------
对照你参考的资料,就看出问题来了,是实体类中的变量的问题。
using System;
/// <summary>
/// ContentText 的摘要说明
/// </summary>
public class ContentText
{
string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
string tel;
public string Tel
{
get { return tel; }
set { tel = value; }
}
string email;
public string Email
{
get { return email; }
set { email = value; }
}
string contents;
public string Contents
{
get { return contents; }
set { contents = value; }
}
string report;
public string Report
{
get { return report; }
set { report = value; }
}
public ContentText()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
用这个,Operator引用时 param.Value = ct.Contents;,注意红色的部分,其他类似。试试吧。