c#中数据连接问题

来源:百度知道 编辑:UC知道 时间:2024/05/26 03:59:18
SqlConnection connStr=new SqlConnection("Server=caty20;uid=sa;pwd=123456;database=goods");
SqlCommand cmd=new SqlCommand(sql,connstr);
if (cmd.Connection.State.ToString()=="Closed"){
cmd.Connection.Open();
}
运行以上代码出错信息为:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.
请问这是什么意思,要怎么解决?在线等
sql="select * from cping";

问题解决了,谢谢大家,原来是数据的访问被我限制了

1。server要保证在SQL Server Configuration Manager里的SQL Server 2005 Network Configuration里,确保TCP/IP的protocol是enable的。
2。client也要保证Native的TCP/IP protocol也enable。
3。当然要确保server的sql server服务已经开启。
4。server如果开了firewall,在exceptions里添加1433 port。

还有你那个判断连接是否已经打开那个最好这样写:
if (connStr.State == ConnectionState.Closed)
{
connStr.Open();
}

SqlCommand cmd=new SqlCommand(sql,connstr); 你这个sql字符串跟哪呢

你上面那段代码没错。。。确定你"Server=caty20;uid=sa;pwd=123456;database=goods");
这都对么。。。下面是我写的测试下 没错。。

SqlConnection con=new SqlConnection("server=.;database=use;uid=sa;pwd=123456");
string sql="select * from iteminfo";
SqlCommand com=new SqlCommand(sql,con);

try
{
if(com.Connection.State.ToString()=="Closed")
{
com.Connection.Open();
this.Response.Write("aaaa");
}