登录数据库验证用户,第一次写csharp,头都有点大
//数据库连接函数
public SqlConnection dbConnect(string server, string db, string uid, string pwd)
{
if (server == null)//传默认参数不知道格式怎么写
server = "(local) ";
if (db == null)
db = "DB_CFGL ";
if (uid == null)
uid = "sa ";
if (pwd == null)
pwd = "houyichong ";
string strconn= "server= "+server+ ";database= "+db+ ";integrated security=false;user id= "+uid+ ";password=houyichong ";
SqlConnection conn = new SqlConnection(strconn);
//conn.ConnectionString =strconn;
conn.ConnectionTimeout = 60;
conn.Open();
return conn;
}
//登录按钮的click事件中调用dbConnect
private void btn_login_Click(object sender, EventArgs e)
{
bool check;
check = checkInput(txt_uid.Text.ToString().Trim()) && checkInput(txt_pwd.Text.ToString());//checkInput检查textbox输入是否为空
if(check==ture)
{
string uid=txt_uid.Text.Trim().ToString();
string pwd=txt_pwd.Text.ToString();
SqlConnection conn = new SqlConnection() ;
conn=dbConnect();//调用函数连接数据库
SqlCommand cmd= conn.CreateCommand() ;
cmd.CommandText = "select 1 from T_user where uid= ' " + uid + " 'and pwd= ' " + pwd + " ' ";
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[ "T_user "].Rows.Count > 0)
{
this.Visible = false;
frmMain.show();
cmd.Dispose();
conn.Close();
conn.Dispose();
ds.Dispose();
da.Dispose();
frm_login.close();
}
else
{
iLogin++;
msgbox( "无效用户名或密码 ", "登录失败 ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);//调用函数msgbox
}
}
else
{
msgbox( "请输入用户名和密码 ", "输入数据 ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);//调用函数msgbox
}
}
//错误:报错的行数正是dbConnect()函数定义的地方
错误1类型“System.Data.SqlClient.SqlConnection”同时存在于“c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll”和“c:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies\System.Data.SqlClient.dll”中D:\C#Practices\helloCsharp\helloCsharp\Form1.cs7817helloCsharp
[解决办法]
把命名空间带上看看
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection (strconn);
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
最新版本:20070212
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
[解决办法]
你可以用VS2005的 TOOLS-〉CONNECT TO DATABASE
连接一下试试。右边的PROPERTY窗口有一个CONNECTION STRING 属性,你对照看看就可以了。
我一般也不记得怎么写CONNSTR,都是这么找的,