int i = int.Parse(rid);
SqlConnection thisconnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\lagenius\JIvandhara ngo\JIvandhara ngo\ngo.mdf;Integrated Security=True;User Instance=True");
thisconnection.Open();
string st = ("select receipt_no, name, rupees, pay_by, date from receipt_info where receipt_no = 4");
DataSet thisdataset = new DataSet();
//string cmdtext = "select * from receipt_info where receipt_no =='" + i + "'";
SqlCommand cmd = new SqlCommand(st, thisconnection);
SqlDataAdapter data_ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
data_ad.Fill(dt);
-4
A
回答
0
string st = ("select receipt_no, name, rupees, pay_by, date from receipt_info where receipt_no =" + i);
0
string st = string.Format("select receipt_no, name, rupees, pay_by, date from receipt_info where receipt_no = {0}",i);
0
首先,你應該使用參數
string st = "select receipt_no, name, rupees, pay_by, date from receipt_info where receipt_no = @Receipt_Number";
重寫你的聲明,您創建當你SqlCommand
- 你應該參數@Receipt_Number增加它
cmd.Parameters.Add("@Receipt_Number", SqlDbType.Int);
cmd.Parameters["@Receipt_Number"].Value = i;
0
發送參數化查詢時,應該使用SqlParameter
。如何使用它們的示例可參見http://www.dotnetperls.com/sqlparameter
基本上,您使用佔位符構造查詢,並使用SqlCommand的s Parameters屬性填充它們。
int searchId = 4;
string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\lagenius\JIvandhara ngo\JIvandhara ngo\ngo.mdf;Integrated Security=True;User Instance=True"
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
using (SqlCommand command = new SqlCommand(
"select receipt_no, name, rupees, pay_by, date " +
"from receipt_info where receipt_no = @Id", connection))
{
command.Parameters.Add(new SqlParameter("Id", searchId));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
}
}
}
相關問題
- 1. 如何在sql查詢中傳遞動態值
- 2. SQL查詢傳遞列名動態
- 3. sql查詢傳遞的動態變量
- 4. 如何在sql中的動態查詢中傳遞datetime?
- 5. 如何在SQL Server 2008中使用動態查詢傳遞表值參數
- 6. 值沒有傳遞給sql server中的動態查詢
- 7. 如何傳遞參數在SQL查詢
- 8. 如何將動態值傳遞給Python的elasticsearch查詢
- 9. 傳遞多個值以在java中動態查詢
- 10. 在查詢中動態傳遞一個值?
- 11. 將參數傳遞給動態查詢
- 12. PL/SQL程序動態傳遞值
- 13. 傳遞java.sql.Date到SQL查詢
- 14. 如何參數值傳遞給T-SQL查詢
- 15. 如何將Dropdownlist值傳遞給SQL查詢?
- 16. 如何傳遞值('運行時已知'多少')到SQL查詢
- 17. 在動態SQL查詢
- 18. 動態SQL查詢
- 19. 動態SQL查詢
- 20. 動態SQL查詢
- 21. 動態SQL查詢
- 22. AngularJS傳遞值查詢API
- 23. 動態SQL逗號分隔值查詢
- 24. 如何使sql查詢動態?
- 25. 如何測試SQL動態查詢?
- 26. 如何動態更新sql查詢 - jquery
- 27. 如何構建動態SQL查詢?
- 28. PHP - 在SQL查詢中傳遞mysql @variable
- 29. 在SQL查詢中傳遞參數
- 30. 如何將動態值傳遞給另一個查看頁面
歡迎計算器!在提問時你應該;提供您的問題的簡短描述,您嘗試的內容,實際和預期結果,然後提出問題。 – Patrick 2014-09-27 07:26:00