0

我也希望能够输入表的名称来添加值。我将它设置Textboxsqlcommand. 我收到“无效对象”错误。我究竟做错了什么。如果我在代码中手动输入表的名称,它可以工作,但是当尝试从它执行它时,textbox它不会。

string tableSelected = stateTextBox.Text + "Geo";
string sqlcomm = ("INSERT INTO [" + tableSelected + "] (Name, Type) VALUES (@Name, @Type)");
SqlConnection conn = new SqlConnection(dbLocation);
SqlCommand comm = new SqlCommand(sqlcomm, conn);
conn.Open();
string[] cities = citiesTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string[] counties = countiesTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string[] federal = positionsFederalTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string[] state = positionsStateTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string[] local = positionsLocalTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
comm.Parameters.AddWithValue("@Name", SqlDbType.NVarChar);
comm.Parameters.AddWithValue("@Type", SqlDbType.NChar);
foreach (string city in cities)
{
    comm.Parameters["@Name"].Value = city;
    comm.Parameters["@Type"].Value = "City";
    comm.ExecuteNonQuery();
}
4

0 回答 0