我試着同時在多線程數據庫上寫數據
但myCommand.Connection.Open()中發生錯誤;
錯誤:未將對象引用設置爲對象的實例。
我該如何解決這個問題?同時在數據庫中插入多行數據c#
這個例子說明問題
private void button1_Click(object sender, EventArgs e)
{
new Thread(() =>
{
SqlCommand myCommand = new SqlCommand("insert into table(a,b)values(1,'aaa')", Connection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}).Start();
new Thread(() =>
{
SqlCommand myCommand = new SqlCommand("insert into table(a,b)values(2,'aaa')", Connection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}).Start();
new Thread(() =>
{
SqlCommand myCommand = new SqlCommand("insert into table(a,b)values(3,'aaa')", Connection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}).Start();
new Thread(() =>
{
SqlCommand myCommand = new SqlCommand("insert into table(a,b)values(4,'aaa')", Connection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}).Start();
}
「Connection」來自哪裏? – dasblinkenlight
這會對服務器造成瓶頸...... – MoonKnight
@Killercam在數據庫中同時插入多行的最佳方法是什麼c# – motaz99