我已经使用这个链接来弄清楚如何做到这一点。但我的问题是它不起作用。这是我的代码:
public void UpdateDatabase(DataSet data, string tableName)
{
string connectionString = ConfigurationManager.ConnectionStrings["TestDbOnBrie"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM dbo.TransportSchedule_Customer;", connection))
{
using (SqlCommand updateCmd = new SqlCommand("UPDATE dbo.TransportSchedule_Customer SET Alias=@Alias, DeliveryDays1=@DeliveryDays1, DeliveryHours1=@DeliveryHours1, DeliveryType1=@DeliveryType1, DeliveryDays2=@DeliveryDays2, DeliveryHours2=@DeliveryHours2, DeliveryType2=@DeliveryType2, DeliveryDays3=@DeliveryDays3, DeliveryHours3=@DeliveryHours3, DeliveryType3=@DeliveryType3, DistanceToDealer=@DistanceToDealer WHERE AdrID=@AdrID AND CustID=@CustID", connection))
{
updateCmd.Parameters.Add("@CustID", SqlDbType.VarChar, 50, "CustID");
updateCmd.Parameters.Add("@AdrID", SqlDbType.Int, 50,"AdrID");
updateCmd.Parameters.Add("@Alias", SqlDbType.VarChar, 50, "Alias");
updateCmd.Parameters.Add("@DeliveryDays1", SqlDbType.VarChar, 50, "DeliveryDays1");
updateCmd.Parameters.Add("@DeliveryHours1", SqlDbType.VarChar, 50, "DeliveryHours1");
updateCmd.Parameters.Add("@DeliveryType1", SqlDbType.VarChar, 50, "DeliveryType1");
updateCmd.Parameters.Add("@DeliveryDays2", SqlDbType.VarChar, 50, "DeliveryDays2");
updateCmd.Parameters.Add("@DeliveryHours2", SqlDbType.VarChar, 50, "DeliveryHours2");
updateCmd.Parameters.Add("@DeliveryType2", SqlDbType.VarChar, 50, "DeliveryType2");
updateCmd.Parameters.Add("@DeliveryDays3", SqlDbType.VarChar, 50, "DeliveryDays3");
updateCmd.Parameters.Add("@DeliveryHours3", SqlDbType.VarChar, 50, "DeliveryHours3");
updateCmd.Parameters.Add("@DeliveryType3", SqlDbType.VarChar, 50, "DeliveryType3");
updateCmd.Parameters.Add("@Alias", SqlDbType.VarChar, 50, "Alias");
adapter.UpdateCommand = updateCmd;
}
}
}
catch (Exception exception)
{
Console.WriteLine("ERROR in UpdateDatabase() method. Error Message : " + exception.Message);
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
}
代码运行没有任何错误,但我的数据库表没有使用新信息进行更新。我可以看到数据集具有从我的 Excel 文件中检索到的正确信息。