如何对存储过程运行数据表以更新数据库

本文关键字:更新 数据库 数据表 运行 存储过程 | 更新日期: 2023-09-27 18:07:00

我使用OleDB导入Excel文件。(没有问题)

我需要连接到数据库并调用一个存储过程,该存储过程本质上是要更新我的数据库。

在数据库中,我需要将主键与其中一列匹配,以便尊重地更新正确的记录。

如果你需要更多的信息,我可以提供。

全新的编码,抱歉,如果这是一个简单的问题或太一般。

目前代码:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
                    foreach (string doc in Directory.GetFiles(@"C:'temp", "*.XLS"))
                    {
                        string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", doc);
                        string workSheet = "Sheet1";
                        var adapter = new OleDbDataAdapter("SELECT * FROM [" + workSheet + "$]", connectionString);
                        var importedExcelTable = new DataTable();
adapter.Fill(importedExcelTable);
                        DataTable newExcelTable = new DataTable();
                        foreach (MileFile row in importedExcelTable.Rows)
                        {
                            DataRow mileFileRow = importedExcelTable.NewRow();
                            mileFileRow["TaxingDistrictID"] = row.TaxingDistrictID;
                            mileFileRow["YearMonth"] = row.YearMonth;
                            mileFileRow["Mileage"] = row.Mileage;
                            mileFileRow["MileageAmount"] = row.MileageAmount;
                            mileFileRow["FipsCode"] = row.FipsCode;
                            newExcelTable.Rows.Add(mileFileRow);
                            SqlConnection connection = new SqlConnection("data source=;database=test;uid=sa;pwd=");
                            SqlCommand selectCommand = new SqlCommand("[job]_[SPROC GOES HERE)", connection);
                            selectCommand.CommandType = CommandType.StoredProcedure;
                            SqlParameter dbParam = selectCommand.Parameters.AddWithValue("@MileFileRow",newExcelTable);
                            dbParam.SqlDbType = SqlDbType.Strucutred;
                        }

如何对存储过程运行数据表以更新数据库

下面是一个基于存储过程更新表的c#方法示例

   using (SqlConnection sqlcon = new SqlConnection(SQLConnectionStringGoesHere))
        {
            using (SqlCommand comm = new SqlCommand("STOREDPROCEDURENAME_Goes_Here", sqlcon))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
                {
                    sqlcon.Open(); //Open the Connection and then begin a SQL Transaction 
                    using (SqlTransaction sqltran = sqlcon.BeginTransaction())
                    {
                        comm.Transaction = sqltran;   //begin tran      
                        comm.CommandType = CommandType.StoredProcedure;
                        comm.Parameters.Add("@OBJECTID", SqlDbType.NVarChar).Value = csvOfIds;
                        comm.Parameters.Add("@ANOTHERPARAMETER", SqlDbType.NVarChar).Value= "TEST";
                        int result = comm.ExecuteNonQuery(); //Execute Query
                        sqltran.Commit();//Commit SQL transaction
                        MessageBox.Show(string.Format("{0} Rows have been affected", result), "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); //Show how many rows were affected
                    }
                }
            }
        }