string connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=save.mdb";
try
{
database = new OleDbConnection(connectionString);
database.Open();
string queryString = "SELECT zivila.naziv,users.user_name FROM (obroki_save "
+ " LEFT JOIN zivila ON zivila.ID=obroki_save.ID_zivila) "
+ " LEFT JOIN users ON users.ID=obroki_save.ID_uporabnika "
+ " WHERE users.ID='" +Convert.ToInt16(id.iDTextBox.Text)+"'";
loadDataGrid(queryString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
答案 0 :(得分:0)
你的connectionstring应如下所示:
Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\ mydatabase.mdb; User Id = admin; Password =;
有关您可以在connectionstrings.com上找到的连接线的更多信息
答案 1 :(得分:0)
在构建sql时,您正在尝试连接字符串和Int16值。尝试删除Convert.ToInt16(),只留下文本框的字符串值。
答案 2 :(得分:0)
尝试将最后一行更改为
+ " WHERE users.ID=" +Convert.ToInt16(id.iDTextBox.Text);
基本上删除了Int16周围的单引号
答案 3 :(得分:0)
你可以试试这个:
string connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=save.mdb";
try
{
Int16 id = Int16.Parse(id.iDTextBox.Text);
database = new OleDbConnection(connectionString);
database.Open();
string queryString = "SELECT zivila.naziv,users.user_name FROM (obroki_save "
+ " LEFT JOIN zivila ON zivila.ID=obroki_save.ID_zivila) "
+ " LEFT JOIN users ON users.ID=obroki_save.ID_uporabnika "
+ " WHERE users.ID=" + id.ToString();
loadDataGrid(queryString);
database.Close();
}
catch (FormatException fex)
{
MessageBox.Show(fex.Message);
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
通过这种方式,您可以拦截不良数据并保护自己免受Sql Injection攻击。
此外,请不要忘记关闭您的连接,或使用using keyword阅读。
还尝试在sql编辑器中使用硬编码的id值运行查询,以确保其有效:
SELECT zivila.naziv,users.user_name
FROM (obroki_save AS os LEFT JOIN zivila AS z ON z.ID=os.ID_zivila)
LEFT JOIN users ON users.ID=obroki_save.ID_uporabnika
WHERE users.ID=16
答案 4 :(得分:0)
如果你的ID字段是int,你不应该把它放在它周围。
所以改变
+ " WHERE users.ID='" +Convert.ToInt16(id.iDTextBox.Text)+"'";
到
+ " WHERE users.ID=" +Convert.ToInt16(id.iDTextBox.Text);