(1)第一种方法:
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDBFilename= |DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T-User where UserName='" + username + "'";
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string dbpassword = reader.GetString(reader.GetOrdinal("password"));
if (password == dbpassword)
{
Console.WriteLine("登陆成功");
}
else
{
Console.WriteLine("密码错误");
}
}
else
{
Console.WriteLine("用户名错误");
}
}
Console.WriteLine("插入成功");
}
}
(2)第二种方法:
把连接字符串写在config文件中
<configuration>
<connectionStrings>
<add name="test" connectionString="Data Source=KINGSOFT-12557\SQLEXPRESS;AttachDBFilename=|DataDirectory|\test.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
</configuration>
登陆按钮事件函数
private void btnLogin_Click(object sender, EventArgs e)
{
DataTable dt = SQLHelper.ExecuteDataTable("select * from T_Users where UserName=@UserName",
new SqlParameter("UserName",txtUser.Text));
if (dt.Rows.Count <= 0)
{
MessageBox.Show("没有这个用户");
}
else
{
DataRow row=dt.Rows[0];
int errorTimes=Convert.ToInt32(row["ErrorTimes"]);
if (errorTimes >= 3)
{
MessageBox.Show("登陆错误次数过多!");
return;
}
string dbPassword = Convert.ToString(row["Password"]);
if (dbPassword.Trim() ==this.txtPassword.Text.Trim())
{
MessageBox.Show("登陆成功");
SQLHelper.ExecuteNonQuery("update T_Users set ErrorTimes=0 where UserName=@UserName",
new SqlParameter("UserName",this.txtUser.Text));
}
else
{
SQLHelper.ExecuteNonQuery("update T_Users set ErrorTimes=ErrorTimes+1 where UserName=@UserName",
new SqlParameter("UserName",this.txtUser.Text));
MessageBox.Show("密码错误");
}
}
}
被调用的函数
/// <summary>
///
/// </summary>
/// params想传多少参数就传多少参数
/// 如: ExecuteNonQuery("insert into t values(@f1,@f2)",p1,p2,p3,p4)
/// <param name="sql"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters)
{
string connStr = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText =sql;
foreach(SqlParameter parameter in parameters )
{
cmd.Parameters.Add(parameter);
}
return cmd.ExecuteNonQuery();
}
}
}
public static DataTable ExecuteDataTable(string sql,params SqlParameter[] parameters)
{
string connstr=ConfigurationManager.ConnectionStrings["test"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using(SqlCommand cmd=conn.CreateCommand())
{
cmd.CommandText = sql;
foreach (SqlParameter parmeter in parameters)
{
cmd.Parameters.Add(parmeter);
}
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
return dataset.Tables[0];
}
}
}
第三种方法:添加数据集方式
登陆按钮事件
private void button1_Click(object sender, EventArgs e)
{
T_UsersTableAdapter adapter = new T_UsersTableAdapter();
强类型登陆.DataSetUsers.T_UsersDataTable users = adapter.GetDataByUserName(txtUserName.Text);
if (users.Count <= 0)
{
MessageBox.Show("用户名错误");
}
else
{
强类型登陆.DataSetUsers.T_UsersRow user=users[0];
if (user.ErrorTimes > 3)
{
MessageBox.Show("错误次数过多");
return;
}
else
{
if (user.Password == txtPassword.Text)
{
MessageBox.Show("登陆成功");
adapter.ResetErrorTimes(user.Id);
}
else
{
adapter.IncErrorTimes(user.Id);
MessageBox.Show("密码错误");
}
}
}
}
被调用的函数都在*xsd中添加,配置文件会在添加此文件时自动添加,不用修改。
这三种方法,第一种适用于大数据量数据的访问,后两种则适用于小数据量的访问。