如何针对存储过程运行数据表来更新数据库?

时间:2014-06-06 14:42:27

标签: c# sql-server excel

我使用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;
                        }

1 个答案:

答案 0 :(得分:-1)

以下是基于存储过程更新表的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
                    }
                }
            }
        }