雙引號不起作用,因此在執行SQL語句直接執行時,必須鍵入'some value'來實際進行變量比較。在ASP.Net中執行SQL語句的問題C#代碼
問題是,現在,當我執行從ASP.NET代碼我不SQL語句似乎得到任何讀數 ...我也不得到錯誤:S ....
I HAVE嘗試自行執行SQL語句,並且確實有效。
public static string testExi(string localIncidentNum)
{
try
{
string query = "SELECT TOP 1 UniqueColID From DBNAME WHERE LocalIncidentNum = @localIncidentNum ORDER BY [version] DESC";
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(connectionStr);
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@localIncidentNum", localIncidentNum);
connection.Open();
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(dt);
connection.Close();
command.Dispose();
connection.Dispose();
if (dt.Rows.Count != 0)
{
string UniqueColID = dt.Rows[0]["UniqueColID"].ToString();
return UniqueColID;
}
else
{
return null;
}
string some = dt.Rows[0]["UniqueColID"].ToString();
return some;
}
catch (Exception err)
{
Global.tmpmsg = " Updating follow up was not successful. " + err.ToString();
return null;
}
}
如果我硬編碼在它的工作原理,但如果我硬編碼在.addwithvalue事件價值,這是行不通的SELECT語句的事件值。
command.Parameters.AddWithValue("@localIncidentNum", "12-023696");
我會建議你使用'using'您SqlConnection對象。 http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.100).aspx – Stephen