导入Excel文件到sql db格式错误 [英] import Excel file to sql db format error

查看:81
本文介绍了导入Excel文件到sql db格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个页面以将数据从excel加载到sql server,我在行
上收到一条错误消息

Hi I am trying to build a page to load data from excel to sql server, I am getting a error message at the line

ProductID = Convert.ToInt32(SSAdapter.InsertProductQuery(PartNumber, Description, Convert.ToDecimal(UnitCost), Convert.ToDecimal(Postage), QtyAvailable.ToInt32, MakeID, DealerID));



说它的格式不正确,任何人都可以提供正确格式的帮助吗?

整个页面是



saying that it is formatted incorrectly can anyone help with the correct format?

the whole page is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.OleDb;
using System.Data.SqlClient;
using CarpartsStore.Data_Access;
using CarpartsStore.Account;
using CarpartsStore;
using CarpartsStore.DataSets;
using System.Data;

namespace CarpartsStore.Dealers
{

    partial class DealerHome : System.Web.UI.Page
    {

        protected void ButtonUpload_Click(object sender, System.EventArgs e)
        {
            PanelUpload.Visible = true;
            PanelView.Visible = false;
            PanelImport.Visible = false;
        }


        protected OleDbCommand ExcelConnection()
        {

            // Connect to the Excel Spreadsheet
            string xConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/ExcelImport.xls") + ";" + "Extended Properties=Excel 8.0;";

            // create your excel connection object using the connection string
            OleDbConnection objXConn = new OleDbConnection(xConnStr);
            objXConn.Open();

            // use a SQL Select command to retrieve the data from the Excel Spreadsheet
            // the "table name" is the name of the worksheet within the spreadsheet
            // in this case, the worksheet name is "Members" and is coded as: [Members$]
            OleDbCommand objCommand = new OleDbCommand("SELECT * FROM [Products$]", objXConn);
            return objCommand;
            
            

        }

        protected void ButtonView_Click(object sender, System.EventArgs e)
        {
            PanelUpload.Visible = false;
            PanelView.Visible = true;
            PanelImport.Visible = false;

            // Create a new Adapter
            OleDbDataAdapter objDataAdapter = new OleDbDataAdapter();

            // retrieve the Select command for the Spreadsheet
            objDataAdapter.SelectCommand = ExcelConnection();

            // Create a DataSet
            DataSet objDataSet = new DataSet();
            // Populate the DataSet with the spreadsheet worksheet data
            objDataAdapter.Fill(objDataSet);

            // Bind the data to the GridView
            GridViewExcel.DataSource = objDataSet.Tables[0].DefaultView;
            GridViewExcel.DataBind();
        }


        protected void ButtonImport_Click(object sender, System.EventArgs e)
        {
            PanelUpload.Visible = false;
            PanelView.Visible = false;
            PanelImport.Visible = true;
            LabelImport.Text = "";
            // reset to blank

            // retrieve the Select Command for the worksheet data
            OleDbCommand objCommand = new OleDbCommand();
            objCommand = ExcelConnection();

            //  create a DataReader
            OleDbDataReader reader;
            reader = objCommand.ExecuteReader();


            //  create variables for the spreadsheet columns
            int ProductID = 0;
            int MakeID = 0;
            int DealerID = 0;
            string PartNumber = "";
            string Description = "";
            string UnitCost = "";
            string Postage = "";
            string QtyAvailable = "";
            string UserName = "";
            string Make = "";


            int counter = 0;
            // used for testing your import in smaller increments

            while (reader.Read())
            {
                counter = counter + 1;
                // counter to exit early for testing...

                // set default values for loop
                ProductID = 0;
                MakeID = 0;
                DealerID = 0;
                


                PartNumber = GetValueFromReader(reader,"PartNumber");
                Description = GetValueFromReader(reader,"Description");

                
                UnitCost = GetValueFromReader(reader,"UnitCost");
                Postage = GetValueFromReader(reader, "Postage"); 
                

                QtyAvailable = GetValueFromReader(reader,"QtyAvailable");
                UserName = GetValueFromReader(reader,"UserName");
                Make = GetValueFromReader(reader,"Make");


                // Insert any required validations here...

                MakeID = GetMakeID(Make);
                DealerID = GetDealerID(UserName); 
                

                //retrieve the MakeID
                ProductID = ImportIntoProducts(PartNumber, Description, UnitCost, Postage, QtyAvailable, MakeID, DealerID);

                LabelImport.Text = LabelImport.Text + ProductID + PartNumber + " " + Description + " " + UnitCost + " " + Postage + " " + QtyAvailable + " " + UserName + " Make_id: " + MakeID + " " + Make + "<br>";
                //If counter > 2 Then ' exit early for testing, comment later...
                //    Exit While
                //End If

            }
            reader.Close();

        }



        protected string GetValueFromReader(OleDbDataReader myreader, string stringValue)
        {
            object val = myreader[stringValue];
            if (val != DBNull.Value)
                return val.ToString();
            else
                return "";
        }


        protected void ButtonUploadFile_Click(object sender, System.EventArgs e)
        {

            if (FileUploadExcel.HasFile)
            {


                try
                {
                    // alter path for your project

                    FileUploadExcel.SaveAs(Server.MapPath("~/ExcelImport.xls"));
                    LabelUpload.Text = "Upload File Name: " +
                        FileUploadExcel.PostedFile.FileName + "<br>" +
                        "Type: " + FileUploadExcel.PostedFile.ContentType +
                        " File Size: " + FileUploadExcel.PostedFile.ContentLength +
                        " kb<br>";
                }
                catch (System.NullReferenceException ex)
                {
                    LabelUpload.Text = "Error: " + ex.Message;
                }
            }
            else
            {
                LabelUpload.Text = "Please select a file to upload.";
            }

        }



        protected int GetMakeID(string MakeName)
        {

            int makeID = 0;
            try
            {
                CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.MakesTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.MakesTableAdapter();
                SSSProductsDataSet.MakesDataTable SSDataTable = null;
                SSDataTable = SSAdapter.GetMakeByName(MakeName);
                // see if the category already exists in the table, if not insert it
                if (SSDataTable != null)
                {
                    if (SSDataTable.Rows.Count > 0)
                    {
                        if (SSDataTable[0].MakeID > 0)
                        {
                            makeID = SSDataTable[0].MakeID;
                        }
                    }
                }
                if (makeID == 0)
                {
                    // if it is still 0, then insert it into the table
                    // retrieve the identity key category_id from the insert
                    makeID = (int)SSAdapter.InsertMakeQuery(MakeName);
                    // if this fails to return the proper category_id, make sure to 
                    // set the InsertCategoryQuery ExecuteMode Property to Scalar
                }
                return makeID;
            }
            catch (System.NullReferenceException ex)
            {
                LabelImport.Text = LabelImport.Text + ex.Message;
                return 0;
            }

        }

        protected int GetDealerID(string UserName)
        {

            int DealerID = 0;
            try
            {
                CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.DealersTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.DealersTableAdapter();
                SSSProductsDataSet.DealersDataTable SSDataTable = null;
                SSDataTable = SSAdapter.GetDealersByUserName(UserName);
                // see if the User already exists in the table, if not insert it
                if (SSDataTable != null)
                {
                    if (SSDataTable.Rows.Count > 0)
                    {
                        if (SSDataTable[0].DealerID > 0)
                        {
                            DealerID = SSDataTable[0].DealerID;
                        }
                    }
                }
                if (DealerID == 0)
                {
                    // if it is still 0, then insert it into the table
                    // retrieve the identity key category_id from the insert
                    DealerID = 0;
                    // if this fails to return the proper category_id, make sure to 
                    // set the InsertCategoryQuery ExecuteMode Property to Scalar
                }
                return DealerID;
            }
            catch (System.NullReferenceException ex)
            {
                LabelImport.Text = LabelImport.Text + ex.Message;
                return 0;
            }

        }


        protected int ImportIntoProducts(string PartNumber, string Description, string UnitCost, string Postage, string QtyAvailable, int MakeID, int DealerID)
        {


            // make sure values don't exceed column limits
            PartNumber = Left(PartNumber, 50);
            Description = Left(Description, 300);
           
            
            


            int ProductID = 0;
            try
            {
                CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.ProductsTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.ProductsTableAdapter();
                SSSProductsDataSet.ProductsDataTable SSDataTable = null;
                SSDataTable = SSAdapter.GetProductsByPartNumberDealer(PartNumber, DealerID);
                // see if the category already exists in the table, if not insert it
                if (SSDataTable != null)
                {
                    if (SSDataTable.Rows.Count > 0)
                    {
                        if (SSDataTable[0].ProductID > 0)
                        {
                            ProductID = SSDataTable[0].ProductID;
                            LabelImport.Text = LabelImport.Text + "<font color=blue>PartNumber Found, Not Imported: " + " ID: " + ProductID + " " + PartNumber + " " + Description + "" + UnitCost + "" + Postage + ".</font><br>";
                        }
                    }
                }
                if (ProductID == 0)
                {
                    // if it is still 0, then insert it into the table
                    // retrieve the identity key ProductID from the insert
                    ProductID = Convert.ToInt32(SSAdapter.InsertProductQuery(PartNumber, Description,Convert.ToDecimal(UnitCost),Convert.ToDecimal(Postage),Convert.ToInt32(QtyAvailable), MakeID, DealerID));
                    LabelImport.Text = LabelImport.Text + "<font color=white>Part Number Imported: " + " ID: " + ProductID + " " + PartNumber + " " + Description + " Cost: " + UnitCost + ".</font><br>";
                }
                return ProductID;
            }
            catch (System.NullReferenceException ex)
            {
                LabelImport.Text = LabelImport.Text + "<font color=red>" + ex.Message + "</font><br>";
                return 0;
            }

        }



        public static string Left(string text, int length)
        {
            if (length < 0)
                throw new ArgumentOutOfRangeException("length", length, "length must be > 0");
            else if (length == 0 || text.Length == 0)
                return "";
            else if (text.Length <= length)
                return text;
            else
                return text.Substring(0, length);

        }

    }
}



非常感谢您的帮助



Would be greatful for any help thanks

推荐答案

OleDbCommand objCommand = OleDbCommand("
OleDbCommand objCommand = new OleDbCommand("SELECT * FROM [Products


",objXConn); 返回 objCommand; } 受保护的 无效 ButtonView_Click(对象发​​件人,系统.EventArgs e) { PanelUpload.Visible = false ; PanelView.Visible = true ; PanelImport.Visible = false ; // 创建新的适配器 OleDbDataAdapter objDataAdapter = OleDbDataAdapter(); // 检索电子表格的Select命令 objDataAdapter.SelectCommand = ExcelConnection(); // 创建数据集 DataSet objDataSet = DataSet(); // 用电子表格工作表数据填充数据集 objDataAdapter.Fill(objDataSet); // 将数据绑定到GridView GridViewExcel.DataSource = objDataSet.Tables [ 0 ].DefaultView; GridViewExcel.DataBind(); } 受保护的 无效 ButtonImport_Click(对象发​​件人,系统.EventArgs e) { PanelUpload.Visible = false ; PanelView.Visible = false ; PanelImport.Visible = true ; LabelImport.Text = " ; // 重置为空白 // 检索工作表数据的选择命令" OleDbCommand objCommand = OleDbCommand(); objCommand = ExcelConnection(); // 创建DataReader OleDbDataReader阅读器; reader = objCommand.ExecuteReader(); // 为电子表格列创建变量 int ProductID = 0 ; int MakeID = 0 ; int DealerID = 0 ; 字符串 PartNumber = " ; 字符串描述= " ; 字符串 UnitCost = " ; string 邮费= " ; 字符串 QtyAvailable = " ; 字符串 UserName = " ; 字符串 Make = " ; int 计数器= 0 ; // 用于以较小的增量测试导入 while (reader.Read()) { counter =计数器+ 1 ; // 计数器将提前退出以进行测试... // 设置循环的默认值 ProductID = 0 ; MakeID = 0 ; DealerID = 0 ; PartNumber = GetValueFromReader(reader," ); 说明= GetValueFromReader(阅读器," ); UnitCost = GetValueFromReader(reader," ); 邮费= GetValueFromReader(阅读器," ); QtyAvailable = GetValueFromReader(reader," ); UserName = GetValueFromReader(reader," ); Make = GetValueFromReader(reader," ); // 在此处插入任何必需的验证... MakeID = GetMakeID(Make); DealerID = GetDealerID(UserName); // 获取MakeID ProductID = ImportIntoProducts(零件编号,描述,单位成本,邮资,可用数量,MakeID,DealerID); LabelImport.Text = LabelImport.Text +产品ID +部件号+ " +描述+ " " + UnitCost + +邮费+ " + QtyAvailable + " +用户名+ + MakeID + " + Make + < br>"; // 如果计数器> 2然后'早点退出进行测试,稍后再评论... // 退出时 // 如果结束 } reader.Close(); } 受保护的 字符串 GetValueFromReader(OleDbDataReader myreader,字符串字符串值) { 对象 val = myreader [stringValue]; 如果(值!= DBNull.Value) 返回 val.ToString(); 其他 返回 " ; } 受保护的 无效 ButtonUploadFile_Click(对象发​​件人,系统.EventArgs e) { 如果(FileUploadExcel.HasFile) { 尝试 { // 更改项目的路径 FileUploadExcel.SaveAs(Server.MapPath(" ))); LabelUpload.Text = " + FileUploadExcel.PostedFile.FileName + " + " + FileUploadExcel.PostedFile.ContentType + " + FileUploadExcel.PostedFile.ContentLength + " ; } 捕获(例如System.NullReferenceException) { LabelUpload.Text = " + ex.Message; } } 其他 { LabelUpload.Text = " ; } } 受保护的 int GetMakeID(字符串 MakeName) { int makeID = 0 ; 尝试 { CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.MakesTableAdapter SSAdapter = CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.MakesTableAdapter(); SSSProductsDataSet.MakesDataTable SSDataTable = ; SSDataTable = SSAdapter.GetMakeByName(MakeName); // 查看表中是否已经存在该类别,如果没有插入 如果(SSDataTable!= ) { 如果(SSDataTable.Rows.Count > 0 ) { 如果(SSDataTable [ 0 ].MakeID ">> 0 ) { makeID = SSDataTable [ 0 ].MakeID; } } } 如果(makeID == 0 ) { // 如果仍为0,则将其插入表格中 // 从插入内容中检索身份密钥category_id makeID =( int )SSAdapter.InsertMakeQuery(MakeName); // 如果此操作无法返回正确的category_id,请确保 // 将InsertCategoryQuery ExecuteMode属性设置为Scalar } 返回 makeID; } 捕获(例如System.NullReferenceException) { LabelImport.Text = LabelImport.Text + ex.Message; 返回 0 ; } } 受保护的 int GetDealerID(字符串用户名) { int DealerID = 0 ; 尝试 { CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.DealersTableAdapter SSAdapter = CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.DealersTableAdapter(); SSSProductsDataSet.DealersDataTable SSDataTable = ; SSDataTable = SSAdapter.GetDealersByUserName(UserName); // 查看用户是否已存在于表中(如果未插入的话) 如果(SSDataTable!= ) { 如果(SSDataTable.Rows.Count > 0 ) { 如果(SSDataTable [ 0 ].DealerID >> 0 ) { DealerID = SSDataTable [ 0 ].DealerID; } } } 如果(经销商ID == 0 ) { // 如果仍为0,则将其插入表格中 // 从插入内容中检索身份密钥category_id DealerID = 0 ; // 如果此操作无法返回正确的category_id,请确保 // 将InsertCategoryQuery ExecuteMode属性设置为Scalar } 返回 DealerID; } 捕获(例如System.NullReferenceException) { LabelImport.Text = LabelImport.Text + ex.Message; 返回 0 ; } } protected int ImportIntoProducts(string PartNumber, string Description, string UnitCost, string Postage, string QtyAvailable, int MakeID, int DealerID) { // make sure values don't exceed column limits PartNumber = Left(PartNumber, 50); Description = Left(Description, 300); int ProductID = 0 ; 尝试 { CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.ProductsTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.ProductsTableAdapter(); SSSProductsDataSet.ProductsDataTable SSDataTable = null; SSDataTable = SSAdapter.GetProductsByPartNumberDealer(PartNumber, DealerID); // see if the category already exists in the table, if not insert it if (SSDataTable != null) { if (SSDataTable.Rows.Count > 0) { if (SSDataTable[0].ProductID > 0) { ProductID = SSDataTable[0].ProductID; LabelImport.Text = LabelImport.Text + "<font color=blue>PartNumber Found, Not Imported: " + " ID: " + ProductID + " " + PartNumber + " " + Description + "" + UnitCost + "" + Postage + ".</font><br>"; } } } if (ProductID == 0) { // if it is still 0, then insert it into the table // retrieve the identity key ProductID from the insert ProductID = Convert.ToInt32(SSAdapter.InsertProductQuery(PartNumber, Description,Convert.ToDecimal(UnitCost),Convert.ToDecimal(Postage),Convert.ToInt32(QtyAvailable), MakeID, DealerID)); LabelImport.Text = LabelImport.Text + "<font color=white>Part Number Imported: " + " ID: " + ProductID + " " + PartNumber + " " + Description + " Cost: " + UnitCost + ".</font><br>"; } 返回产品ID; } catch (System.NullReferenceException ex) { LabelImport.Text = LabelImport.Text + "<font color=red>" + ex.Message + "</font><br>"; 返回 0 ; } } public static string Left(string text, int length) { if (length < 0) throw new ArgumentOutOfRangeException("length", length, "length must be > 0"); else if (length == 0 || text.Length == 0) 返回 " ; else if (text.Length <= length) 返回文本; 其他 return text.Substring(0, length); } } }
", objXConn); return objCommand; } protected void ButtonView_Click(object sender, System.EventArgs e) { PanelUpload.Visible = false; PanelView.Visible = true; PanelImport.Visible = false; // Create a new Adapter OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(); // retrieve the Select command for the Spreadsheet objDataAdapter.SelectCommand = ExcelConnection(); // Create a DataSet DataSet objDataSet = new DataSet(); // Populate the DataSet with the spreadsheet worksheet data objDataAdapter.Fill(objDataSet); // Bind the data to the GridView GridViewExcel.DataSource = objDataSet.Tables[0].DefaultView; GridViewExcel.DataBind(); } protected void ButtonImport_Click(object sender, System.EventArgs e) { PanelUpload.Visible = false; PanelView.Visible = false; PanelImport.Visible = true; LabelImport.Text = ""; // reset to blank // retrieve the Select Command for the worksheet data OleDbCommand objCommand = new OleDbCommand(); objCommand = ExcelConnection(); // create a DataReader OleDbDataReader reader; reader = objCommand.ExecuteReader(); // create variables for the spreadsheet columns int ProductID = 0; int MakeID = 0; int DealerID = 0; string PartNumber = ""; string Description = ""; string UnitCost = ""; string Postage = ""; string QtyAvailable = ""; string UserName = ""; string Make = ""; int counter = 0; // used for testing your import in smaller increments while (reader.Read()) { counter = counter + 1; // counter to exit early for testing... // set default values for loop ProductID = 0; MakeID = 0; DealerID = 0; PartNumber = GetValueFromReader(reader,"PartNumber"); Description = GetValueFromReader(reader,"Description"); UnitCost = GetValueFromReader(reader,"UnitCost"); Postage = GetValueFromReader(reader, "Postage"); QtyAvailable = GetValueFromReader(reader,"QtyAvailable"); UserName = GetValueFromReader(reader,"UserName"); Make = GetValueFromReader(reader,"Make"); // Insert any required validations here... MakeID = GetMakeID(Make); DealerID = GetDealerID(UserName); //retrieve the MakeID ProductID = ImportIntoProducts(PartNumber, Description, UnitCost, Postage, QtyAvailable, MakeID, DealerID); LabelImport.Text = LabelImport.Text + ProductID + PartNumber + " " + Description + " " + UnitCost + " " + Postage + " " + QtyAvailable + " " + UserName + " Make_id: " + MakeID + " " + Make + "<br>"; //If counter > 2 Then ' exit early for testing, comment later... // Exit While //End If } reader.Close(); } protected string GetValueFromReader(OleDbDataReader myreader, string stringValue) { object val = myreader[stringValue]; if (val != DBNull.Value) return val.ToString(); else return ""; } protected void ButtonUploadFile_Click(object sender, System.EventArgs e) { if (FileUploadExcel.HasFile) { try { // alter path for your project FileUploadExcel.SaveAs(Server.MapPath("~/ExcelImport.xls")); LabelUpload.Text = "Upload File Name: " + FileUploadExcel.PostedFile.FileName + "<br>" + "Type: " + FileUploadExcel.PostedFile.ContentType + " File Size: " + FileUploadExcel.PostedFile.ContentLength + " kb<br>"; } catch (System.NullReferenceException ex) { LabelUpload.Text = "Error: " + ex.Message; } } else { LabelUpload.Text = "Please select a file to upload."; } } protected int GetMakeID(string MakeName) { int makeID = 0; try { CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.MakesTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.MakesTableAdapter(); SSSProductsDataSet.MakesDataTable SSDataTable = null; SSDataTable = SSAdapter.GetMakeByName(MakeName); // see if the category already exists in the table, if not insert it if (SSDataTable != null) { if (SSDataTable.Rows.Count > 0) { if (SSDataTable[0].MakeID > 0) { makeID = SSDataTable[0].MakeID; } } } if (makeID == 0) { // if it is still 0, then insert it into the table // retrieve the identity key category_id from the insert makeID = (int)SSAdapter.InsertMakeQuery(MakeName); // if this fails to return the proper category_id, make sure to // set the InsertCategoryQuery ExecuteMode Property to Scalar } return makeID; } catch (System.NullReferenceException ex) { LabelImport.Text = LabelImport.Text + ex.Message; return 0; } } protected int GetDealerID(string UserName) { int DealerID = 0; try { CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.DealersTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.DealersTableAdapter(); SSSProductsDataSet.DealersDataTable SSDataTable = null; SSDataTable = SSAdapter.GetDealersByUserName(UserName); // see if the User already exists in the table, if not insert it if (SSDataTable != null) { if (SSDataTable.Rows.Count > 0) { if (SSDataTable[0].DealerID > 0) { DealerID = SSDataTable[0].DealerID; } } } if (DealerID == 0) { // if it is still 0, then insert it into the table // retrieve the identity key category_id from the insert DealerID = 0; // if this fails to return the proper category_id, make sure to // set the InsertCategoryQuery ExecuteMode Property to Scalar } return DealerID; } catch (System.NullReferenceException ex) { LabelImport.Text = LabelImport.Text + ex.Message; return 0; } } protected int ImportIntoProducts(string PartNumber, string Description, string UnitCost, string Postage, string QtyAvailable, int MakeID, int DealerID) { // make sure values don't exceed column limits PartNumber = Left(PartNumber, 50); Description = Left(Description, 300); int ProductID = 0; try { CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.ProductsTableAdapter SSAdapter = new CarpartsStore.DataSets.SSSProductsDataSetTableAdapters.ProductsTableAdapter(); SSSProductsDataSet.ProductsDataTable SSDataTable = null; SSDataTable = SSAdapter.GetProductsByPartNumberDealer(PartNumber, DealerID); // see if the category already exists in the table, if not insert it if (SSDataTable != null) { if (SSDataTable.Rows.Count > 0) { if (SSDataTable[0].ProductID > 0) { ProductID = SSDataTable[0].ProductID; LabelImport.Text = LabelImport.Text + "<font color=blue>PartNumber Found, Not Imported: " + " ID: " + ProductID + " " + PartNumber + " " + Description + "" + UnitCost + "" + Postage + ".</font><br>"; } } } if (ProductID == 0) { // if it is still 0, then insert it into the table // retrieve the identity key ProductID from the insert ProductID = Convert.ToInt32(SSAdapter.InsertProductQuery(PartNumber, Description,Convert.ToDecimal(UnitCost),Convert.ToDecimal(Postage),Convert.ToInt32(QtyAvailable), MakeID, DealerID)); LabelImport.Text = LabelImport.Text + "<font color=white>Part Number Imported: " + " ID: " + ProductID + " " + PartNumber + " " + Description + " Cost: " + UnitCost + ".</font><br>"; } return ProductID; } catch (System.NullReferenceException ex) { LabelImport.Text = LabelImport.Text + "<font color=red>" + ex.Message + "</font><br>"; return 0; } } public static string Left(string text, int length) { if (length < 0) throw new ArgumentOutOfRangeException("length", length, "length must be > 0"); else if (length == 0 || text.Length == 0) return ""; else if (text.Length <= length) return text; else return text.Substring(0, length); } } }



Would be greatful for any help thanks



Would be greatful for any help thanks


You can directly import into Sql Database from Excel like this

http://www.excel-sql-server.com/excel-import-to-sql-server-using-distributed-queries.htm[^]
You can directly import into Sql Database from Excel like this

http://www.excel-sql-server.com/excel-import-to-sql-server-using-distributed-queries.htm[^]


这篇关于导入Excel文件到sql db格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆