将值插入 Excel 时收到错误消息(错误消息:查询值和目标字段的数量不同)。有谁能帮我解决这个错误。此代码的目标是从 sql 数据库中获取值并将其插入 Excel。
//代码
string sheetname = comboBox1.SelectedItem.ToString();
int s = 1, w = 0;
string ssss = "";
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
xlapp.Visible = true;
xlapp.Workbooks.Open(@"\\server\planet\CalculationDatabase\Database.xls");
string path = @"\\server\planet\CalculationDatabase\Database.xls";
string path1 = @"\\server\planet\CalculationDatabase\Settings.xlsx";
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + @";Extended Properties=""Excel 12.0 Macro;HDR=YES""";
string con1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path1 + @";Extended Properties=""Excel 12.0 Macro;HDR=YES""";
OleDbConnection cn2 = new OleDbConnection(con1);
cn2.Open();
OleDbCommand cmd2 = new OleDbCommand("select * from [" + sheetname.ToString() + "$]", cn2);
OleDbDataReader rd2 = cmd2.ExecuteReader();
while (rd2.Read())
{
ssss = rd2["Info Name"].ToString();
if (ssss.ToString() == "")
{
w = int.Parse(s.ToString());
break;
}
s++;
}
rd2.Close();
cn2.Close();
var queries = string.Join(",", (Enumerable.Range(1, w).Select(i => "ip" + i)));
SqlConnection con3 = new SqlConnection("Data Source=173.83.250.71,1433; Network Library=DBMSSOCN; Initial Catalog=paradigm;User Id=sa; password=Paradigm123");
con3.Open();
SqlCommand cmd3 = new SqlCommand("select " + queries.ToString() + " from paradigm.dbo.pinput where tempsheet='" + comboBox1.SelectedItem.ToString() + "'", con3);
SqlDataReader rd3 = cmd3.ExecuteReader();
OleDbConnection cn = new OleDbConnection(con);
cn.Open();
while (rd3.Read())
{
var queries1 = string.Join(",", (Enumerable.Range(0, w - 1).Select(i => "'" + float.Parse(rd3[i].ToString()) + "'")));
cmd1.ExecuteNonQuery();//I am getting error here
}
rd3.Close();
con3.Close();
}