我想将设备名称和相关序列号的字符串列表插入SQL Server 2014上的表中。IDE为Visual Studio 2015,编程语言为C#。

当我运行程序并单击button14时,错误是:

参数化查询'(@ID int,@ NAME nvarchar(4000),@ RSSI int)INSERT BeaconInfo(ID,N'期望提供未提供的参数'@NAME'。

我不确定我插入sql的代码是否正确。

public partial class Form1 : Form
{
    int seqnumber = 333;
    List<string> items;
    string tmp_name;
    BluetoothDeviceInfo[] devices;

    public Form1()
    {
       items = new List<string>();
       InitializeComponent();
    }

private void startScan()
{
    listBox1.DataSource = null;
    listBox1.Items.Clear();
    items.Clear();
    Thread bluetoothScanThread = new Thread(new ThreadStart(scan));
    bluetoothScanThread.Start();
}

private void scan()
{
    updateUI("Starting Scan..");
    BluetoothClient client = new BluetoothClient();
    devices = client.DiscoverDevicesInRange();
    updateUI("Scan complete");
    updateUI(devices.Length.ToString() + " devices discovered");

    foreach (BluetoothDeviceInfo d in devices)
    {
      items.Add(d.DeviceName);
    }
    updateDeviceList();
}


private void button14_Click(object sender, EventArgs e)
{
    System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("....");
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT BeaconInfo (ID, Name, RSSI) VALUES (@ID, @NAME, @RSSI)";
    cmd.Parameters.AddWithValue("@ID", seqnumber);
    cmd.Parameters.AddWithValue("@NAME", tmp_name);
    cmd.Parameters.AddWithValue("@RSSI", 55);
    cmd.Connection = sqlConnection1;

    for (int j = 0; j < items.Count; j++)
    {
      seqnumber = seqnumber + 1;
      tmp_name = items[j];
      sqlConnection1.Open();
      cmd.ExecuteNonQuery();
      sqlConnection1.Close();
    }
}

===============>>#1 票数:1 已采纳

请尝试更改您的代码,如下所示:

private void button14_Click(object sender, EventArgs e)
{
    using (System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("....")) 
    {

       sqlConnection1.Open();

       for (int j = 0; j < items.Count; j++)
       {
           seqnumber = seqnumber + 1;
           tmp_name = items[j];



           using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) 
           {
              cmd.CommandType = System.Data.CommandType.Text;
              cmd.CommandText = "INSERT BeaconInfo (ID, Name, RSSI) VALUES (@ID, @NAME, @RSSI)";
              cmd.Parameters.AddWithValue("@ID", seqnumber);
              cmd.Parameters.AddWithValue("@NAME", tmp_name);
              cmd.Parameters.AddWithValue("@RSSI", 55);
              cmd.Connection = sqlConnection1;


              try 
              {
                   cmd.ExecuteNonQuery();
              } 
              catch (Exception ex) 
              {
                   //Here handle if error
              }

           }
        }
    }
}

一般而言,在处理程序中编写sql语句不是一种好习惯,因此在Form qctually中使用。 最好创建执行此操作的类,然后在处理程序中调用它。

请注意,在您提出的问题下的注释中,未设置tmp_name且SqlClient上的参数未引用,因此在与变量tmp_name关联后更改值不会对参数值产生任何影响。

  ask by QuickLearner translate from so

未解决问题?本站智能推荐: