ContentText.contents 不可访问,因为它受保护级别限制

nan450 2012-07-19 10:35:06
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 怎么也不行 啊 求指导
...全文
233 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nan450 2012-07-19
  • 打赏
  • 举报
回复
哇 太厉害了 真的非常感谢 真的谢谢你啊 成功了 太感谢了!
happytonice 2012-07-19
  • 打赏
  • 举报
回复
对照你参考的资料,就看出问题来了,是实体类中的变量的问题。

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;,注意红色的部分,其他类似。试试吧。

nan450 2012-07-19
  • 打赏
  • 举报
回复
还是没用啊 我这代码是在 http://www.docin.com/p-107483941.html#documentinfo
看来的 文档10.3.1里的代码 我现在在学习 可是就是不理解啊
lizhengdao1314 2012-07-19
  • 打赏
  • 举报
回复
ContentText类中的contents属性的get定义成了private或是protected,或是没有get。
public class ContentText
{
string userName;
string tel;
string email;
string contents;
string report;
public ContentText()
{
//
//TODO: 在此处添加构造函数逻辑
//

}
Public Contents{get{return contents;}set{contents=value;}}
}
nan450 2012-07-19
  • 打赏
  • 举报
回复
public class ContentText
{
string userName;
string tel;
string email;
string contents;
string report;
public ContentText()
{
//
//TODO: 在此处添加构造函数逻辑
//

}
}

ContentText在另一个类里定义过了 有public的
happytonice 2012-07-19
  • 打赏
  • 举报
回复
ContentText 是如何定义的 加没加public?属性如何定义的?
lizhengdao1314 2012-07-19
  • 打赏
  • 举报
回复
ContentText.contents是不是只有set属性,没有get属性,或get属性定义成私有

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧