C# 抛出异常:';System.Data.SqlClient.SqlException';在System.Data.dll中
如果我错过了某个地方的答案,我会提前道歉,但我在任何地方都找不到答案。因此,我正在构建一个应用程序,它扫描我们公司获得的服务订单的PDF,解析它,并将其插入SQL数据库。问题就在这段代码的末尾。它成功地:C# 抛出异常:';System.Data.SqlClient.SqlException';在System.Data.dll中,c#,sql-server,C#,Sql Server,如果我错过了某个地方的答案,我会提前道歉,但我在任何地方都找不到答案。因此,我正在构建一个应用程序,它扫描我们公司获得的服务订单的PDF,解析它,并将其插入SQL数据库。问题就在这段代码的末尾。它成功地: 将原始pdf保存在适当的文件夹中 扫描pdf并解析它 将正确的数据插入服务订单表 获取刚刚创建的服务订单的PK,因为我们需要该PK用于下一批插入 在这里,它会被抛出异常挂起:System.Data.dll中的“System.Data.SqlClient.SqlException” 我检查了
- 将原始pdf保存在适当的文件夹中
- 扫描pdf并解析它
- 将正确的数据插入服务订单表
- 获取刚刚创建的服务订单的PK,因为我们需要该PK用于下一批插入
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/PDF/") + filename);
// Now we parse the PDF by creating a new ServiceOrder object and parsing from it.
ServiceOrder so = new ServiceOrder();
// Make sure we load the PDF from the correct path on the server
so.LoadPDF(Server.MapPath("~/PDF/") + filename);
String strConnString = "Data Source=127.0.0.0;Initial Catalog=SOMECATALOG;User ID=SOMEUSER;Password=SOMEPASSWORD";
// Insert Into Service Orders Table
string defaultdate = DateTime.Now.ToString("yyyy-MM-dd");
String strQuery = "insert into TServiceOrders (strServiceOrderNo, intStatusCodeID, strCustomerName, strCustomerNo, strCustomerAddress1, strCustomerAddress2, strCustomerAddress3, intRepID, strServiceDescription, strServiceRequestDate, strServiceOrderDate, strNotes) values ('"
+ so.ServiceOrderNumber.ToString() + "', 2, '"
+ so.CustomerContactName.ToString() + "', '"
+ so.CustomerNumber.ToString() + "', '"
+ so.CustomerContactAddress1.ToString() + "', '"
+ so.CustomerContactAddress2.ToString() + "', '"
+ so.CustomerContactAddress3.ToString() + "', 1, '', '"
+ defaultdate + "', '" + defaultdate + "', '')";
SqlConnection conn = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(strQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
// Grabbing latest primary key od service order just added for next instrument inserts
int lastid = 999999;
String strPKquery = "select top 1 intServiceOrderID from TServiceOrders order by intServiceOrderID desc";
SqlDataReader rdr = null;
SqlConnection conn2 = new SqlConnection(strConnString);
SqlCommand cmd2 = new SqlCommand(strPKquery, conn2);
try
{
conn2.Open();
rdr = cmd2.ExecuteReader();
while (rdr.Read())
{
lastid = (int)rdr["intServiceOrderID"];
}
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn2 != null)
{
conn2.Close();
}
}
// Insert Into Service Instruments Tables
SqlConnection conn3 = new SqlConnection(strConnString);
conn3.Open();
foreach (ServiceInstrument sin in so.ServiceInstruments)
{
string sim = "";
sim = sin.ServiceInstrumentModel;
if (String.IsNullOrEmpty(sim))
{
sim = "";
}
else
{
sim = sin.ServiceInstrumentModel.ToString();
}
string sid = "";
sid = sin.ServiceInstrumentDescription;
if (String.IsNullOrEmpty(sid))
{
sid = "";
}
else
{
sid = sin.ServiceInstrumentDescription.ToString();
}
string sis = "";
sis = sin.ServiceInstrumentSerial;
if (String.IsNullOrEmpty(sis))
{
sis = "";
}
else
{
sis = sin.ServiceInstrumentSerial.ToString();
}
string sih = "";
sih = sin.ServiceInstrumentHandle;
if (String.IsNullOrEmpty(sih))
{
sih = "";
}
else
{
sih = sin.ServiceInstrumentHandle.ToString();
}
string sip = "";
sip = sin.ServiceInstrumentParentAsset;
if (String.IsNullOrEmpty(sip))
{
sip = "";
}
else
{
sip = sin.ServiceInstrumentParentAsset.ToString();
}
String strQuery3 = "insert into TServiceInstruments values ('" + sim.ToString() + "', '" + sid.ToString() + "', '" + sis.ToString() + "', '" + sih.ToString() + "', " + sip.ToString() + ", " + lastid + ")";
SqlCommand cmd3 = new SqlCommand(strQuery3, conn3);
cmd3.ExecuteNonQuery();
}
conn3.Close();
也许这不配成为一个答案,但我正在努力建立一些声誉,所以这里是:) 我怀疑您的错误在于“插入到TServiceInstruments…”语句中。也就是说,您为表提供了更多(或更少)列。作为一种良好做法,请始终指定列,如下所示:
insert into TServiceInstruments (column1, column2, column3)
values (1, 2, 3)
和语句,以确保即使发生异常,它们也会关闭
最后一点注意:您确实需要学习如何读取异常,这包括堆栈跟踪,它直接指向异常发生的调用堆栈中的行。如果您能理解这一点,那么调试就容易多了。我是否错过了您所说的引发异常的行?发布整个异常。另外,使用参数而不是将字符串串联在一起。这不仅仅是一个SQL注入漏洞,它是一个bug农场。此外,也没有必要用所有那些
ToString()
调用来搞糟它。C#可以弄清楚如何进行字符串化。此外,我会在这里使用ExecuteScalar()
而不是ExecuteReader()
,因为您只返回一条记录。还记得.PDF是一种格式的那些日子吗?如果您不想分析数据,请添加一个关于SQL注入的PSA,我将删除否决票。使用参数化查询代码,您的答案会有很大的改进…谢谢,但我的表中有7列,它是自动递增的,所以有6个插入。我试图嵌入一些图片,但它不让我。当我在它上面加一个中断并检查它时,语句看起来很好,插入了正确的数据
const String strQuery3 = "INSERT INTO TServiceInstruments (sim, sid, sis, sih, sip, lid) VALUES (@sim, @sid, @sis, @sih, @sip, @lid)";
using(var conection = new SqlConnection(strConnString))
using(SqlCommand command = new SqlCommand(strQuery3, connection))
{
command.Parameters.Add(new SqlParameter("@sim", SqlDbType.VarChar, 200){Value = sim});
command.Parameters.Add(new SqlParameter("@sid", SqlDbType.VarChar, 200){Value = sid});
command.Parameters.Add(new SqlParameter("@sis", SqlDbType.VarChar, 200){Value = sis});
command.Parameters.Add(new SqlParameter("@sih", SqlDbType.VarChar, 200){Value = sih});
command.Parameters.Add(new SqlParameter("@sip", SqlDbType.Int){Value = sip});
command.Parameters.Add(new SqlParameter("@lid", SqlDbType.Int){Value = lid});
connection.Open();
command.ExecuteNonQuery();
}