我連接了一個.MDF
數據庫文件到C#(Visual Studio)中的aspx文件,但是當我試圖執行查詢時,它不起作用。在asp.net中連接的SQL Server數據庫文件,但查詢未執行
下面給出了問題出現的代碼。請幫助我。
的com.ExecuteNonQuery()
返回-1,我想它應該在的情況下返回成功執行
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\karunya\Documents\Visual Studio 2010\Projects\Login\Login\App_Data\MyDB.mdf;Integrated Security=True;User Instance=True");
con.Open();
string sql = "SELECT * FROM Data WHERE user = '"+user.Text+"' ";
SqlCommand com = new SqlCommand(sql, con);
int tmp = Convert.ToInt32(com.ExecuteNonQuery());
con.Close();
if (tmp == 1)
{
con.Open();
sql = "SELECT pass FROM LogTable WHERE user = '" + user.Text + "'";
SqlCommand com1 = new SqlCommand(sql, con);
string password = com1.ExecuteScalar().ToString();
if (password == pass.Text)
{
Response.Write("Access Granted!");
}
else
{
Response.Write("Access Denied!");
}
}
else
{
Response.Write("User Name Incorrect!");
}
'ExecuteNonQuery()'返回**受到DELETE,INSERT或UPDATE操作影響的**行數。既然你沒有這樣做,那麼它總是返回-1 - 這是** NOT **錯誤條件! [這是**所有**很好,並在MSDN上廣泛記錄!](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery%28v=vs.110%29 .aspx) - 你只需要**去尋找它!** – 2014-11-08 08:54:52
除了@ marc_s的評論,受影響的行的數量嚴格依賴於使用的遊標類型 – Alireza 2014-11-08 08:59:06
@Arun:我會建議你使用executablecalar if你正在檢查單個記錄。並通過您的選擇查詢,似乎你正在檢查用戶是否存在。我對嗎?如果你是請不要在select語句中使用*。 – 2014-11-08 09:30:36