将excelsheet字段转换为datetime数据类型并插入到sql表中

时间:2012-11-02 12:26:21

标签: c# .net

我有一个excel文件,其中包含一个字段名称logingdate。 现在我将数据从excel表传输到sql表,但是有一个问题。

如何在sql表的日期时间数据类型中转换excel表的logingdate字段。

我想将excel表数据存储在已提交数据类型为datetime的sql表中。    这是代码。

    string strFileName = Path.GetFileName(ddlExcelFile.SelectedValue);
    OleDbConnection oconn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/ExcelSheets/" + strFileName) + ";Extended Properties='Excel 12.0;HDR=YES';");
    try
    {
        //After connecting to the Excel sheet here we are selecting the data
        //using select statement from the Excel sheet
        OleDbCommand ocmd = new OleDbCommand("select * from [System Users$]", oconn);
        oconn.Open(); //Here [Sheet1$] is the name of the sheet
        //in the Excel file where the data is present
        OleDbDataReader odr = ocmd.ExecuteReader();
        string LoginID = "";
        string FirstName = "";
        string LastName = "";
        string Email = "";
        string IsActive="";
        string LastUpdateOn;
        string ProjectAccess = "";
        while (odr.Read())
        {
            LoginID = valid(odr, 0);//Here we are calling the valid method
            FirstName = valid(odr, 1);
            LastName = valid(odr, 2);
            Email = valid(odr, 3);
            IsActive = valid(odr, 4);
            LastUpdateOn = valid(odr, 5);
            ProjectAccess = valid(odr, 6);
            //Here using this method we are inserting the data into the database
            insertdataintosql(LoginID, FirstName, LastName, Email, IsActive, LastUpdateOn, ProjectAccess);
        }
        oconn.Close();
    }
    catch (DataException ee)
    {
        lblmsg.Text = ee.Message;
        lblmsg.ForeColor = System.Drawing.Color.Red;
    }
    finally
    {
        lblmsg.Text = "Data Inserted Sucessfully";
        lblmsg.ForeColor = System.Drawing.Color.Green;
    }
}

protected string valid(OleDbDataReader myreader, int stval)//if any columns are
//found null then they are replaced by zero
{
    if(stval==4)
    {
        object val = myreader[stval];

        if (Convert.ToInt32(val)==1)
            return Convert.ToString(true);
        else
            return Convert.ToString(false);
    }        
    else
    {
        object val = myreader[stval];
        if (val != DBNull.Value)
            return val.ToString();
        else
            return Convert.ToString(0);
    }
}

public void insertdataintosql(string LoginID, string FirstName, string LastName, string Email, string IsActive, string LastUpdateOn, string ProjectAccess)
{//inserting data into the Sql Server
    SqlConnection conn = new SqlConnection("Data Source=NET07;Integrated Security=False;Initial Catalog=IrisDevon; User Id=sa; password=kk;");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "insert into ImportSystemUsers(UserName,FirstName,LastName,Email,IsActive,LastUpdateOn)values(@UserName,@FirstName,@LastName,@Email,@IsActive,@LastUpdateOn)";
    cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = LoginID;
    cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = FirstName;
    cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = LastName;
    cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = Email;
    cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Value =Convert.ToBoolean(IsActive);
    string dt1 = Convert.ToDateTime(LastUpdateOn.Trim()).ToString();
    cmd.Parameters.Add("@LastUpdateOn", SqlDbType.DateTime).Value = dt1;//error here
    cmd.Parameters.Add("@ProjectAccess", SqlDbType.NVarChar).Value = ProjectAccess;
    cmd.CommandType = CommandType.Text;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}
正如我在发生错误的代码中提到的那样。

0 个答案:

没有答案