我以前有一个带有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;
}
这就是新的Insert函数的样子,我无法弄清楚为什么数据不再插入数据库?!?!?!?
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;
}
}
答案 0 :(得分:1)
您的第一个代码段包含执行命令的调用,但第二个代码不包含。在关闭之前添加一个调用来执行sql命令。
cmd.Parameters.AddWithValue("@CreateDate", Convert.ToDateTime(dr.CreateDate));
int result = cmd.ExecuteNonQuery(); // This line is missing
con.Close();