“connectionstring 属性尚未初始化”的问题

zk911 2005-11-17 02:58:21
我的代码如下:
private Base db = new Base();
private login c=new login() ;
try
{
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = db.connStr;
string strCom = "SELECT * FROM admin WHERE username='" + c.loginuser +"'";
System.Data.SqlClient.SqlCommand comm;
comm = myConn.CreateCommand();
comm.CommandText = strCom;
SqlDataAdapter mydataA= new SqlDataAdapter();
mydataA.SelectCommand = comm;
DataSet myDataSet = new DataSet ( ) ;
myConn.Open( );
mydataA.Fill(myDataSet,"admin");
myConn.Close ( ) ;
}
catch(System.Exception mass)
{
MessageBox.Show(mass.Message);
}

当运行到myConn.open();时 则提示"connectionstring 属性尚未初始化"
这个问题该如何解决
请各路朋友指点一下 谢谢
...全文
308 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
holym 2005-11-17
  • 打赏
  • 举报
回复
myConn.ConnectionString = db.strConn;
或者
SqlConnection myConn = new SqlConnection("Data Source=com;Initial Catalog=rsgl;uid=sa;pwd=sa ");

3tzjq 2005-11-17
  • 打赏
  • 举报
回复
试试:
SqlConnection myConn = new SqlConnection(@"Data Source=com;Initial Catalog=rsgl;User Id=sa;Password=sa;");
......
zk911 2005-11-17
  • 打赏
  • 举报
回复
把 永不言弃 的代码复制进去后 运行之
出现的提示还是一样的
这是什么原因啊
请朋友们指点一下

小弟谢过了
zhy0101 2005-11-17
  • 打赏
  • 举报
回复
难道是那个卖书的网站
jxzhang615 2005-11-17
  • 打赏
  • 举报
回复
3tzjq 2005-11-17
  • 打赏
  • 举报
回复
db.connStr 是否有值?
try
{
SqlConnection myConn = new SqlConnection(db.connStr);
Console.Writeline(myConn.ConnectionString);//有正确连接字符串?
string strCom = "SELECT * FROM admin WHERE username='" + c.loginuser +"'";
SqlDataAdapter mydataA= new SqlDataAdapter(strCom,myConn);

DataSet myDataSet = new DataSet ( ) ;
mydataA.Fill(myDataSet,"admin");
}
catch(System.Exception mass)
{
MessageBox.Show(mass.Message);
}
jxufewbt 2005-11-17
  • 打赏
  • 举报
回复
myConn.ConnectionString = db.connStr;
改成
myConn.ConnectionString = db.strConn;
jxufewbt 2005-11-17
  • 打赏
  • 举报
回复
connStr在Base类中应该声明为public
zk911 2005-11-17
  • 打赏
  • 举报
回复
类代码如下:
base 类:
#region 属性
public static string strConn = @"Data Source=com;Initial Catalog=rsgl;User Id=sa;Password=sa;" ;

protected static string strSql ;

#endregion

#region 方法

public static int ExcuteSql(string strSql)
{
SqlConnection myConn = new SqlConnection (strConn);
SqlCommand myCommand = new SqlCommand(strSql,myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
return 0;
}
catch ( System.Data.SqlClient.SqlException e )
{
throw new Exception( e.Message );
}
finally
{
myCommand.Dispose();
myConn.Close();
}
}
public static string connyh;
public string connStr
{

set
{
connyh=value;
}
get
{
return connyh;
}
..........


login类:
public static string loginstr;
public string loginuser
{
set
{
loginstr = value;
}
get
{
return loginstr;
}
}

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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