C# 添加数据到数据库有关问题
C# 添加数据到数据库问题。
为什么这段代码添加到数据库之后,明明是按列添加的,可是从第二列开始的时候,却不是第一行开始写的,而是从前一列的最后一行接下去写?代码如下:
for (int rst = 0; rst <= i; rst++)
{
if (temp[rst, 2] != null)
{
string strAd = "insert into allinfo("+xian[k,1]+") values('" + "K" + "-" + temp[rst, 2] + "')";
SqlCommand Acomm = new SqlCommand(strAd, sqlCon);
Acomm.ExecuteNonQuery();
}
if (temp[rst, 1] != null)//输出的设备编号
{
string strAdd = "insert into allinfo(" + xian[k, 1] + ") values('" + "X" + "-" + temp[rst, 1] + "')";
string stradd = "insert into allinfo(" + xian[k, 1] + ") values('" + "B" + "-" + temp[rst, 0] + "')";
SqlCommand ACcomm = new SqlCommand(strAdd, sqlCon);
ACcomm.ExecuteNonQuery();
}
}
表中的数据变成这样了。。



------解决思路----------------------
检查所有带索引的地方,估计是索引错位了
------解决思路----------------------
你这个是循环执行insert语句, 然后你要看xian[k, 1] 的值是什么 K也是变量他代表的 字段名是不同的 ,仔细看看就能找到问题,打断点 把所有 SQL 语句都 贴到一起 对比一下就出来了
为什么这段代码添加到数据库之后,明明是按列添加的,可是从第二列开始的时候,却不是第一行开始写的,而是从前一列的最后一行接下去写?代码如下:
for (int rst = 0; rst <= i; rst++)
{
if (temp[rst, 2] != null)
{
string strAd = "insert into allinfo("+xian[k,1]+") values('" + "K" + "-" + temp[rst, 2] + "')";
SqlCommand Acomm = new SqlCommand(strAd, sqlCon);
Acomm.ExecuteNonQuery();
}
if (temp[rst, 1] != null)//输出的设备编号
{
string strAdd = "insert into allinfo(" + xian[k, 1] + ") values('" + "X" + "-" + temp[rst, 1] + "')";
string stradd = "insert into allinfo(" + xian[k, 1] + ") values('" + "B" + "-" + temp[rst, 0] + "')";
SqlCommand ACcomm = new SqlCommand(strAdd, sqlCon);
ACcomm.ExecuteNonQuery();
}
}
表中的数据变成这样了。。
------解决思路----------------------
检查所有带索引的地方,估计是索引错位了
------解决思路----------------------
你这个是循环执行insert语句, 然后你要看xian[k, 1] 的值是什么 K也是变量他代表的 字段名是不同的 ,仔细看看就能找到问题,打断点 把所有 SQL 语句都 贴到一起 对比一下就出来了