62,041
社区成员
发帖
与我相关
我的任务
分享
string source = "Data Source=DEMI_ZHANG20\\CHENDB;Initial Catalog=testDB;Persist Security Info=True;User ID=sa;Password=1234567";
string sql = "select * from table1 where username='" + TextBox1.Text.Trim() + "'";
SqlConnection conn = new SqlConnection(source);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader userRead = cmd.ExecuteReader();
if (userRead != null)
{
while (userRead.Read())
{
if (TextBox2.Text.Trim() == userRead.GetValue(0).ToString())
{
Label3.Text = "ok";
return;
}
else
{
Label3.Text = "password error";
return;
}
}
}
else
{
Label3.Text = "There is no this username";
}
userRead.Close();
conn.Close();
}
catch (OleDbException oe)
{
Label3.Text = oe.Errors[0].Message;
}
if(……)//if 1
{
while (……)
{
if(……)//if 2
}
}
//下面的這些可以寫到class裡面去,然後在頁面中調用。
public static readonly string connstr = ConfigurationManager.ConnectionStrings["你想寫什麽就寫什麽"].ToString();
public static SqlConnection myconn = new SqlConnection(connstr);
public static System.Data.SqlClient.SqlCommand cmd;
//關閉鏈接
public void sqlconn_Close()
{
if (myconn.State == ConnectionState.Open)
{
myconn.Close();
//myconn.Dispose();
}
}
//打開數據庫鏈接
public void sqlconn_Open()
{
if (myconn.State == ConnectionState.Closed)
{
// myconn.Close();
myconn.Open();
}
}
//連接數據庫的一個sql
public void sqlcomm(string mysqlstring)
{
cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = myconn;
cmd.CommandText = mysqlstring;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
}
public string UserCheck(string userName, string password)
{
try
{
sqlcomm("usp_UserCheck_select");
SqlParameter param = new SqlParameter();
param.Direction = System.Data.ParameterDirection.ReturnValue;
cmd.Parameters.Add(param);
cmd.Parameters.AddWithValue("@userName", userName);
cmd.Parameters.AddWithValue("@password",password);
//sqlconn_Open();我記得好像是不要寫的,不確定,LZ調試時自己多留意下:)
cmd.ExecuteNonQuery();
sqlconn_Close();
return param.Value.ToString();
}
catch (Exception ex)
{
return "Error" + ex.Message;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if(UserCheck(textbox1.text.trim(), textbox2.text.trim())=="1")
{//成功}
else{//失敗}
}
--存儲過程你就這樣寫
create proc usp_UserCheck_select
@userName nvarchar(50),
@password nvarchar(50)
AS
BEGIN
if exists (select 1 from public_item where username=@username and password=@password)
return 1
END