Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t使用c在循环中将数据插入SQL Server表#_C#_Sql Server - Fatal编程技术网

C# Can';t使用c在循环中将数据插入SQL Server表#

C# Can';t使用c在循环中将数据插入SQL Server表#,c#,sql-server,C#,Sql Server,如屏幕截图所示,我试图将数据插入表中。第一次迭代工作正常,但第二次迭代会引发错误/异常 我的代码有什么问题 下面是代码 SqlConnection sqlconn = new SqlConnection(sqlconnectionstring); // sqlconn.Open(); string InsertData = "INSERT INTO AUStagAPITestData ([TestSuite], [TestCase],[Status], [Info], [Time], [Is

如屏幕截图所示,我试图将数据插入表中。第一次迭代工作正常,但第二次迭代会引发错误/异常

我的代码有什么问题

下面是代码

SqlConnection sqlconn = new SqlConnection(sqlconnectionstring);
//  sqlconn.Open();

string InsertData = "INSERT INTO AUStagAPITestData ([TestSuite], [TestCase],[Status], [Info], [Time], [IsArchived], [DateTime]) VALUES (@TestSuite, @TestCase, @Status, @Info, @Time, @IsArchived, @DateTime)";

SqlCommand Insertcmd = new SqlCommand(InsertData, sqlconn);

for (int j = 1; j < TDData.Length; j +=5)
{
    sqlconn.Open();

    string TestSuite = TDData[j];
    string TestCase = TDData[j+1];
    string Status = TDData[j + 2];
    string Info = TDData[j + 3];
    string Time = TDData[j + 4];

    Insertcmd.Parameters.AddWithValue("@TestSuite", TestSuite);
    Insertcmd.Parameters.AddWithValue("@TestCase", TestCase);
    Insertcmd.Parameters.AddWithValue("@Status", Status);
    Insertcmd.Parameters.AddWithValue("@Info", Info);
    Insertcmd.Parameters.AddWithValue("@Time", Time);
    Insertcmd.Parameters.AddWithValue("@IsArchived", "1");
    Insertcmd.Parameters.AddWithValue("@DateTime", DateTime.Now);

    Insertcmd.ExecuteNonQuery();
    sqlconn.Close();
}
SqlConnection sqlconn=newsqlconnection(sqlconnectionstring);
//sqlconn.Open();
string InsertData=“插入到AUStagAPITestData([TestSuite]、[TestCase]、[Status]、[Info]、[Time]、[IsArchived]、[DateTime])值(@TestSuite、@TestCase、@Status、@Info、@Time、@IsArchived、@DateTime)”;
SqlCommand Insertcmd=新的SqlCommand(InsertData,sqlconn);
对于(int j=1;j

它抱怨您已经添加了:

Insertcmd.Parameters.AddWithValue("@TestSuite
修复方法是在每次迭代中实例化一个新的
SqlCommand

for (int j = 1; j < TDData.Length; j +=5) 
{
sqlconn.Open(); 
SqlCommand Insertcmd = new SqlCommand(InsertData, sqlconn); 
string TestSuite= TDData[j];
for(int j=1;j
它抱怨您已经添加了:

Insertcmd.Parameters.AddWithValue("@TestSuite
修复方法是在每次迭代中实例化一个新的
SqlCommand

for (int j = 1; j < TDData.Length; j +=5) 
{
sqlconn.Open(); 
SqlCommand Insertcmd = new SqlCommand(InsertData, sqlconn); 
string TestSuite= TDData[j];
for(int j=1;j
尝试将sqlconn.Open()和sqlconn.Close()移出for循环并续订sqlcommand对象

sqlconn.Open();
for (int j = 1; j < TDData.Length; j += 5)
{
    SqlCommand Insertcmd = new SqlCommand(InsertData, sqlconn);
    string TestSuite = TDData[j];
    ...
}
sqlconn.Close();
sqlconn.Open();
对于(int j=1;j
尝试将sqlconn.Open()和sqlconn.Close()移出for循环并续订sqlcommand对象

sqlconn.Open();
for (int j = 1; j < TDData.Length; j += 5)
{
    SqlCommand Insertcmd = new SqlCommand(InsertData, sqlconn);
    string TestSuite = TDData[j];
    ...
}
sqlconn.Close();
sqlconn.Open();
对于(int j=1;j
根据您的代码检查此示例:

string connectionString, queryInsert;
string[] arrayData = new string[10];

connectionString = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString;
queryInsert = @"
    INSERT INTO AUStagAPITestData
    (
        [TestSuite], [TestCase], [Status], [Info], [Time], [IsArchived], [DateTime]
    )
    VALUES (
        @TestSuite, @TestCase, @Status, @Info, @Time, @IsArchived, @DateTime
    )
";

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(queryInsert, connection))
{
    string testSuite, testCase, status, info, time;

    connection.Open();

    for (int j = 1; j < arrayData.Length; j += 5)
    {
        testSuite = arrayData[j];
        testCase = arrayData[j + 1];
        status = arrayData[j + 2];
        info = arrayData[j + 3];
        time = arrayData[j + 4];

        command.Parameters.AddWithValue("@TestSuite", testSuite);
        command.Parameters.AddWithValue("@TestCase", testCase);
        command.Parameters.AddWithValue("@Status", status);
        command.Parameters.AddWithValue("@Info", info);
        command.Parameters.AddWithValue("@Time", time);
        command.Parameters.AddWithValue("@IsArchived", "1");
        command.Parameters.AddWithValue("@DateTime", DateTime.Now);

        command.ExecuteNonQuery();

        // To Clear parameters
        command.Parameters.Clear();
    }
    // no need to close a disposed object since dispose will call close 
}
字符串连接字符串,queryInsert;
string[]arrayData=新字符串[10];
connectionString=ConfigurationManager.connectionString[“DbConn”]。connectionString;
queryInsert=@“
插入到AUStagAPITestData中
(
[TestSuite]、[TestCase]、[Status]、[Info]、[Time]、[IsArchived]、[DateTime]
)
价值观(
@TestSuite、@TestCase、@Status、@Info、@Time、@IsArchived、@DateTime
)
";
使用(SqlConnection连接=新的SqlConnection(connectionString))
使用(SqlCommand=newsqlcommand(queryInsert,connection))
{
字符串testSuite、testCase、状态、信息、时间;
connection.Open();
对于(int j=1;j
根据您的代码检查此示例:

string connectionString, queryInsert;
string[] arrayData = new string[10];

connectionString = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString;
queryInsert = @"
    INSERT INTO AUStagAPITestData
    (
        [TestSuite], [TestCase], [Status], [Info], [Time], [IsArchived], [DateTime]
    )
    VALUES (
        @TestSuite, @TestCase, @Status, @Info, @Time, @IsArchived, @DateTime
    )
";

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(queryInsert, connection))
{
    string testSuite, testCase, status, info, time;

    connection.Open();

    for (int j = 1; j < arrayData.Length; j += 5)
    {
        testSuite = arrayData[j];
        testCase = arrayData[j + 1];
        status = arrayData[j + 2];
        info = arrayData[j + 3];
        time = arrayData[j + 4];

        command.Parameters.AddWithValue("@TestSuite", testSuite);
        command.Parameters.AddWithValue("@TestCase", testCase);
        command.Parameters.AddWithValue("@Status", status);
        command.Parameters.AddWithValue("@Info", info);
        command.Parameters.AddWithValue("@Time", time);
        command.Parameters.AddWithValue("@IsArchived", "1");
        command.Parameters.AddWithValue("@DateTime", DateTime.Now);

        command.ExecuteNonQuery();

        // To Clear parameters
        command.Parameters.Clear();
    }
    // no need to close a disposed object since dispose will call close 
}
字符串连接字符串,queryInsert;
string[]arrayData=新字符串[10];
connectionString=ConfigurationManager.connectionString[“DbConn”]。connectionString;
queryInsert=@“
插入到AUStagAPITestData中
(
[TestSuite]、[TestCase]、[Status]、[Info]、[Time]、[IsArchived]、[DateTime]
)
价值观(
@TestSuite、@TestCase、@Status、@Info、@Time、@IsArchived、@DateTime
)
";
使用(SqlConnection连接=新的SqlConnection(connectionString))
使用(SqlCommand=newsqlcommand(queryInsert,connection))
{
字符串testSuite、testCase、状态、信息、时间;
connection.Open();
对于(int j=1;j
您可以在for循环中使用
Insertcmd.Parameters.Clear()

您可以在for循环中使用
Insertcmd.Parameters.Clear()

您真正应该做的是:

  • 在循环之前创建一次参数对象列表
  • 在循环过程中,仅设置其
大概是这样的: