|
|
|
เรียกใช้ connection แล้วมัน error ไม่ทราบว่าเรียกใช้ยังไง C#, VS 2010 (.NET 4.x) |
|
|
|
|
|
|
|
Code (C#)
ConnectTools ConnectTool = new ConnectTools();
protected void Page_Load(object sender, EventArgs e)
{
bool blnSttus = ConnectTool.Connected("AcerPC", "sa", "1234", "ProjectDB");
if (blnSttus == true)
{
lblStatus.Text = "SQL CONNECTTION SUCCESS!";
}
else if (blnSttus == false)
{
lblStatus.Text = "SQL CONNECTTION FAIL!";
}
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string user = Login1.UserName;
string pwd = Login1.Password;
string sqlUserName;
string sqlUserName2;
string sqlUserName3;
sqlUserName = "SELECT UTypeID, UserID, Password FROM User";
sqlUserName += "WHERE (UserID = @UserName ";
sqlUserName += "AND Password =@Password AND UTypeID = '01')";
sqlUserName2 = "SELECT UTypeID, UserID, Password FROM User";
sqlUserName2 += "WHERE (UserID = @UserName ";
sqlUserName2 += "AND Password =@Password AND UTypeID = '02')";
sqlUserName3 = "SELECT UTypeID, UserID, Password FROM User";
sqlUserName3 += "WHERE (UserID = @UserName ";
sqlUserName3 += "AND Password =@Password AND UTypeID = '03')";
SqlCommand com = new SqlCommand(sqlUserName, ConnectTool);
SqlCommand com2 = new SqlCommand(sqlUserName2, ConnectTool);
SqlCommand com3 = new SqlCommand(sqlUserName3, ConnectTool);
Tag : ASP.NET C#, VS 2010 (.NET 4.x)
|
|
|
|
|
|
Date :
2012-06-27 10:21:46 |
By :
Nan Tuesday |
View :
1105 |
Reply :
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ต้องการเรียกใช้ ConnectTool แต่มัน error ตรงสามบรรทัดสุดท้ายอ่ะค่ะ ต้องเรียกใช้ยังไงคะ
|
|
|
|
|
Date :
2012-06-27 10:23:45 |
By :
Nan Tuesday |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดู datasouce ใน class ConnectTools ก่อนครับว่าถูกหรือไม่
|
|
|
|
|
Date :
2012-06-27 11:11:18 |
By :
kanchen |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ConnectTools
{
private SqlConnection Conn;
public Boolean Connected(String Server, String User, String Password, String Database)
{
string ConnectionString;
bool Isconnect = false;
ConnectionString = String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", Server, Database, User, Password);
Conn = new SqlConnection(ConnectionString);
try
{
Conn.Open();
Isconnect = true;
}
catch
{
Isconnect = false;
}
return Isconnect;
}
}
คิดว่าถูกนะคะ เพราะมันขึ้น SQL CONNECTTION SUCCESS! อ่ะ แต่คิดว่าเรียกใช้ไม่ถูก
|
|
|
|
|
Date :
2012-06-27 12:00:56 |
By :
Nan Tuesday |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอดู error ที่ขึ้นหน่อยครับ
|
|
|
|
|
Date :
2012-06-27 13:09:26 |
By :
kanchen |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2012-06-27 14:11:36 |
By :
Nan Tuesday |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
private SqlConnection sqlConnection;
protected void Page_Load(object sender, EventArgs e)
{
bool blnSttus;
sqlConnection = ConnectTool.Connected("AcerPC", "sa", "1234", "ProjectDB", out blnSttus);
if (blnSttus == true)
{
lblStatus.Text = "SQL CONNECTTION SUCCESS!";
}
else if (blnSttus == false)
{
lblStatus.Text = "SQL CONNECTTION FAIL!";
}
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string user = Login1.UserName;
string pwd = Login1.Password;
string sqlUserName;
string sqlUserName2;
string sqlUserName3;
sqlUserName = "SELECT UTypeID, UserID, Password FROM User";
sqlUserName += "WHERE (UserID = @UserName ";
sqlUserName += "AND Password =@Password AND UTypeID = '01')";
sqlUserName2 = "SELECT UTypeID, UserID, Password FROM User";
sqlUserName2 += "WHERE (UserID = @UserName ";
sqlUserName2 += "AND Password =@Password AND UTypeID = '02')";
sqlUserName3 = "SELECT UTypeID, UserID, Password FROM User";
sqlUserName3 += "WHERE (UserID = @UserName ";
sqlUserName3 += "AND Password =@Password AND UTypeID = '03')";
SqlCommand com = new SqlCommand(sqlUserName, sqlConnection);
SqlCommand com2 = new SqlCommand(sqlUserName2, sqlConnection);
SqlCommand com3 = new SqlCommand(sqlUserName3, sqlConnection);
}
Code (C#)
public class ConnectTools
{
private SqlConnection Conn;
public SqlConnection Connected(String Server, String User, String Password, String Database, out bool Result)
{
string ConnectionString;
ConnectionString = String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", Server, Database, User, Password);
Conn = new SqlConnection(ConnectionString);
try
{
Conn.Open();
Result = true;
}
catch
{
Result = false;
}
return Conn;
}
}
|
|
|
|
|
Date :
2012-06-27 14:56:39 |
By :
อิอิ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ส่งค่าไปด้วยสิครับ ใน page load คุณเขียนแบบนี้ ConnectTool.Connected("AcerPC", "sa", "1234", "ProjectDB");
แต่ใน Login1_Authenticate คุณใส่ class ConnectTool ไปมันก็ไม่ได้สิครับ คุณต้องเรียก method ที่ connectionstring ด้วยครับ
เหมือนใน page load ไงครับ
|
|
|
|
|
Date :
2012-06-27 14:59:24 |
By :
kanchen |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอรบกวนอีกนิดคะ จะแก้ไขยังไงคะ
|
|
|
|
|
Date :
2012-06-27 16:05:10 |
By :
Tuesday |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|