无法在我的SQL表中插入数值

时间:2016-01-14 12:50:02

标签: sql database insert

我目前在我的数据库中有2个表,1个具有名为ZoekcriteriaID的自动数字值,这是Zoekcriteria表中的主键。

我想将主键(ZoekcriteriaID)添加到名为Resultaten的另一个表作为forgeign键,但我一直收到同样的错误。

似乎cmd1.Parameters.AddWithValue("@ZoekcriteriaID",Convert.ToInt32(sqlZoekcriteriaID));一直试图将整个查询添加为数值,我似乎无法弄清楚原因。

有人可以帮助我吗?

namespace Proftaak
{
class Mysearch

{
    public  string zoekterm = "";
    int resultaat = 1;
    public string Zoekterm
    {
        get
        {
            return zoekterm;
        }
        set
        {
            zoekterm = value;
        }
    }

    public void InsertZoekcriteria()
    {
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\martijn\Dropbox\Proftaak Periode 2 Identity\Database11.accdb;
        Persist Security Info=False;";
        connection.Open();
        string sqlstring = "INSERT INTO Zoekcriteria (ZoekCriteria) values ('" + Zoekterm + "')";

        OleDbCommand cmd = new OleDbCommand(sqlstring, connection);
        cmd.ExecuteNonQuery();
        connection.Close();
    }
    public void searchding()
    {


        const string apiKey = "AIzaSyDIm9ZOWD8Zd-2tHy5r3c0R-_XjdEFaXGE";
        const string searchEngineId = "003470263288780838160:ty47piyybua";
        string query = zoekterm;
        CustomsearchService customSearchService = new CustomsearchService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = apiKey });
        Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = customSearchService.Cse.List(query);
        listRequest.Cx = searchEngineId;
        Search search = listRequest.Execute();
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\martijn\Dropbox\Proftaak Periode 2 Identity\Database11.accdb;
        Persist Security Info=False;";
        connection.Open();
        string sqlZoekcriteriaID = "SELECT ZoekcriteriaID from Zoekcriteria where Zoekcriteria='" + query + "'";
        OleDbCommand cmdZoekcriteria = new OleDbCommand(sqlZoekcriteriaID, connection);
        cmdZoekcriteria.ExecuteNonQuery();

          foreach (var item in search.Items)
        {


            string sqlstring1 = @"INSERT INTO Resultaat (ResultatenID,ZoekcriteriaID, Titel, Webadress) VALUES (@ResultatenID,@ZoekcriteriaID, @Titel, @Webadress)";

          OleDbCommand cmd1 = new OleDbCommand(sqlstring1, connection);
          cmd1.Parameters.AddWithValue("@ResultatenID", resultaat);
          cmd1.Parameters.AddWithValue("@ZoekcriteriaID",Convert.ToInt32(sqlZoekcriteriaID));
          cmd1.Parameters.AddWithValue("@Titel", item.Title);
          cmd1.Parameters.AddWithValue("@Webadress", item.Link);
          //    string sqlstring2= "INSERT INTO Resultaat(Titel) values ('"+item.Title+"')";
     //     OleDbCommand cmd2 = new OleDbCommand(sqlstring2, connection);
          resultaat++;
          cmd1.ExecuteNonQuery();
        }


        connection.Close(); 

2 个答案:

答案 0 :(得分:0)

那是因为它确实:

  string sqlZoekcriteriaID = "SELECT ZoekcriteriaID from Zoekcriteria where Zoekcriteria='" + query + "'";
  cmd1.Parameters.AddWithValue("@ZoekcriteriaID",Convert.ToInt32(sqlZoekcriteriaID));

您想要的是使用查询的结果。你应该做的是在this问题的答案中描述的。

您还应该重新考虑变量的命名; query不是查询,例如,只是搜索条件。

答案 1 :(得分:0)

在关闭连接之前,请恢复身份编号,如下所示:

OleDbCommand cmd = new OleDbCommand(sqlstring, connection);
        cmd.ExecuteNonQuery();


   cmd.CommandText = "SELECT @@IDENTITY";
   integer ZoekcriteriaID = cmd.ExecuteScalar()

  connection.Close();

您可以在另一个插页上使用ZoekcriteriaID。

请参阅此link