数据未通过 WCF 插入到 DB
Data not inserting to DB through WCF
我曾经有一个带有 dataContracts 的服务,这样可以完美地将数据插入数据库,这是代码:
public string InsertDetails(DeviceDetails deviceInfo)
{
using (SqlConnection con = new SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
{
string Message;
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO VR_Image (IMEINo, FileName, FileURL, Uploader, CarrierID, StorageName, FileSize, CreateDate)
values (@IMEINo, @FileName, @FileURL, @Uploader, @CarrierID, @StorageName, @FileSize, @CreateDate)", con);
cmd.Parameters.AddWithValue("@IMEINo", deviceInfo.IMEINo);
cmd.Parameters.AddWithValue("@FileName", deviceInfo.FileName);
cmd.Parameters.AddWithValue("@FileURL", deviceInfo.FileURL);
cmd.Parameters.AddWithValue("@Uploader", deviceInfo.Uploader);
cmd.Parameters.AddWithValue("@CarrierID", deviceInfo.CarrierID);
cmd.Parameters.AddWithValue("@StorageName", deviceInfo.StorageName);
cmd.Parameters.AddWithValue("@FileSize", deviceInfo.FileSize);
cmd.Parameters.AddWithValue("@CreateDate", deviceInfo.CreateDate);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = deviceInfo.IMEINo + " Details inserted successfully";
}
else
{
Message = deviceInfo.IMEINo + " Details not inserted successfully";
}
con.Close();
return Message;
}
}
我现在更改了服务以从客户端接收数据集(客户端应用程序和服务在这里受控,因此即使数据集不是可行的方法,但它可以工作,因为我们的用户有限)。我只有一个 operationContract 将处理所有传入的数据集并将它们分发到它需要去的特定函数,这就是 operationContract 函数的代码:
public DataSet InterfaceService(string sInterface, DataSet dsIn)
{
System.Data.DataSet dsClean = new System.Data.DataSet();
try
{
MethodInfo oMethod = null;
oMethod = this.GetType().GetMethod(sInterface);
System.Data.DataSet dsOut = (System.Data.DataSet)oMethod.Invoke(this, new object[] { dsIn });
dsClean.Merge(dsOut);
}
catch (Exception ex)
{
throw new FaultException(ex.Message, new FaultCode("An error has occurred. Please try again."));
}
return dsClean;
}
这就是新插入函数的样子,我不明白为什么数据不再插入到数据库中?!?!?!?
public DataSet InsertDetails(DataSet dsIn)
{
DataSet1.InsertInfoDataTable ds = new DataSet1.InsertInfoDataTable();
ds.Merge(dsIn.Tables["InsertInfo"]);
DataSet1.InsertInfoRow dr = ds.First();
using (SqlConnection con = new SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO VR_Image (IMEINo, FileName, FileURL, Uploader, CarrierID, StorageName, FileSize, CreateDate)
VALUES (@IMEINo, @FileName, @FileURL, @Uploader, @CarrierID, @StorageName, @FileSize, @CreateDate)", con);
cmd.Parameters.AddWithValue("@IMEINo", dr.IMEINo);
cmd.Parameters.AddWithValue("@FileName", dr.FileName);
cmd.Parameters.AddWithValue("@FileURL", dr.FileURL);
cmd.Parameters.AddWithValue("@Uploader", dr.Uploader);
cmd.Parameters.AddWithValue("@CarrierID", dr.CarrierID);
cmd.Parameters.AddWithValue("@StorageName", dr.StorageName);
cmd.Parameters.AddWithValue("@FileSize", Convert.ToInt32(dr.FileSize));
cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
con.Close();
return dsIn;
}
}
您的第一个代码片段包含执行命令的调用,但第二个代码片段没有。添加调用以在关闭之前执行 sql 命令。
cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
int result = cmd.ExecuteNonQuery(); // This line is missing
con.Close();
我曾经有一个带有 dataContracts 的服务,这样可以完美地将数据插入数据库,这是代码:
public string InsertDetails(DeviceDetails deviceInfo)
{
using (SqlConnection con = new SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
{
string Message;
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO VR_Image (IMEINo, FileName, FileURL, Uploader, CarrierID, StorageName, FileSize, CreateDate)
values (@IMEINo, @FileName, @FileURL, @Uploader, @CarrierID, @StorageName, @FileSize, @CreateDate)", con);
cmd.Parameters.AddWithValue("@IMEINo", deviceInfo.IMEINo);
cmd.Parameters.AddWithValue("@FileName", deviceInfo.FileName);
cmd.Parameters.AddWithValue("@FileURL", deviceInfo.FileURL);
cmd.Parameters.AddWithValue("@Uploader", deviceInfo.Uploader);
cmd.Parameters.AddWithValue("@CarrierID", deviceInfo.CarrierID);
cmd.Parameters.AddWithValue("@StorageName", deviceInfo.StorageName);
cmd.Parameters.AddWithValue("@FileSize", deviceInfo.FileSize);
cmd.Parameters.AddWithValue("@CreateDate", deviceInfo.CreateDate);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = deviceInfo.IMEINo + " Details inserted successfully";
}
else
{
Message = deviceInfo.IMEINo + " Details not inserted successfully";
}
con.Close();
return Message;
}
}
我现在更改了服务以从客户端接收数据集(客户端应用程序和服务在这里受控,因此即使数据集不是可行的方法,但它可以工作,因为我们的用户有限)。我只有一个 operationContract 将处理所有传入的数据集并将它们分发到它需要去的特定函数,这就是 operationContract 函数的代码:
public DataSet InterfaceService(string sInterface, DataSet dsIn)
{
System.Data.DataSet dsClean = new System.Data.DataSet();
try
{
MethodInfo oMethod = null;
oMethod = this.GetType().GetMethod(sInterface);
System.Data.DataSet dsOut = (System.Data.DataSet)oMethod.Invoke(this, new object[] { dsIn });
dsClean.Merge(dsOut);
}
catch (Exception ex)
{
throw new FaultException(ex.Message, new FaultCode("An error has occurred. Please try again."));
}
return dsClean;
}
这就是新插入函数的样子,我不明白为什么数据不再插入到数据库中?!?!?!?
public DataSet InsertDetails(DataSet dsIn)
{
DataSet1.InsertInfoDataTable ds = new DataSet1.InsertInfoDataTable();
ds.Merge(dsIn.Tables["InsertInfo"]);
DataSet1.InsertInfoRow dr = ds.First();
using (SqlConnection con = new SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO VR_Image (IMEINo, FileName, FileURL, Uploader, CarrierID, StorageName, FileSize, CreateDate)
VALUES (@IMEINo, @FileName, @FileURL, @Uploader, @CarrierID, @StorageName, @FileSize, @CreateDate)", con);
cmd.Parameters.AddWithValue("@IMEINo", dr.IMEINo);
cmd.Parameters.AddWithValue("@FileName", dr.FileName);
cmd.Parameters.AddWithValue("@FileURL", dr.FileURL);
cmd.Parameters.AddWithValue("@Uploader", dr.Uploader);
cmd.Parameters.AddWithValue("@CarrierID", dr.CarrierID);
cmd.Parameters.AddWithValue("@StorageName", dr.StorageName);
cmd.Parameters.AddWithValue("@FileSize", Convert.ToInt32(dr.FileSize));
cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
con.Close();
return dsIn;
}
}
您的第一个代码片段包含执行命令的调用,但第二个代码片段没有。添加调用以在关闭之前执行 sql 命令。
cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
int result = cmd.ExecuteNonQuery(); // This line is missing
con.Close();