c# 怎么创建登陆界面的数据库连接啊

详细点

你要别人回答详细点,你自己问题又不写详细别人呢怎么详细? 你的数据库是什么数据库? Access还是SqlServer还是其他什么,是本地的还是远程的,你所谓的C#是Web的(ASP.NET)还是WinForm或者WPF的?

举个例子给你吧, SqlServer2005 Express版, ASP.NET 2.0 位于 App_Data文件夹内的数据库文件AAA.mdf,数据库名dbname, 则创建连接的代码是:

SqlConnection cn = new SqlConnection("Server=.\SQLExpress;AttachDbFilename=|DataDirectory|AAA.mdf; Database=dbname;Trusted_Connection=Yes;");
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-07-11
其实用不着这么麻烦!可以设一个登陆按钮就可以了!以下是具体代码:
在C#中登陆按钮代码

private void button1_Click(object sender, System.EventArgs e)
{
switch(comboBox1.SelectedIndex)
{
case 0:
if(textBox1.Text==""||textBox2.Text=="")
{
MessageBox.Show("用户名和密码不能为空","提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
return;
}
else
{
try
{
OleDbConnection conn=db.createdb();
conn.Open();
OleDbCommand cmd=new OleDbCommand("select count(*) from user_info where user_id='"+textBox1.Text+"'and user_pwd='"+textBox2.Text+"'and beizhu='1'",conn);
int count=Convert.ToInt32(cmd.ExecuteScalar().ToString());
if(count>0)
{
Form2 form=new Form2();
form.Show();
this.Hide();

}
}
catch(Exception ex)
{
MessageBox.Show("错误"+ex.ToString(),"错误");
}

}
break;
default:
break;

}

登陆:

public class Users
{
private string username="";
private string password="";
public string UserName
{
get{return this.username;}
set{this.username=value;}
}
public string Password
{
get{return this.password;}
set{this.password=value;}
}
public string CheckUser()
{
SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["ConStr"]);
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="select UserName,Password from Users where UserName=@uname";
cmd.Parameters.Add("@uname",SqlDbType.VarChar,10);
cmd.Parameters[0].Value=UserName;
string pass="";
try
{
con.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
pass=reader[1].ToString();
}
else
{
reader.Close();
con.Close();
return UserName+"用户存在!";
}
reader.Close();
con.Close();
}
catch(SqlException er)
{
if(con.State==ConnectionState.Open)
con.Close();
return er.Message;
}
if(pass.Trim()!=Password.Trim())
{
return "对不起!"+UserName+"用户密码不正确!";
}

return "OK";
}
}
相似回答
大家正在搜