I have used this link to figure out how to do this. But my problem is that it does not work. This is my code:
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();
}
}
}
}
The code runs without any errors, but my database table does not get updated with the new information. I can see that the Dataset has the right information which it retrieves from my Excel file.
you forget to call Update
method for updating data in you cod that might be a problem here
check the link give by you there is method used for update like this
sqlDa.Update(dSet,"emp");
which do update in database.
MSDN
DataAdapter.Update Method - Calls the respective INSERT, UPDATE, or DELETE
statements for each inserted, updated, or deleted
row in the specified DataSet
from a DataTable
named " Table
."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.