实际上,我需要将所有数据从excel插入到oracle数据库。在下面的程序中,我正在使用对象数组存储表中使用的所有不同类型的数据类型。这样做时,我无法将空值存储在对象数组中。
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
/* string ex_name = "";
string ex_school = "";
string ex_id = "";
string ex_college = "";
string ex_office = ""; */
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
//string str;
int rCnt;
int cCnt;
int rw = 0;
int cl = 0, i = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\Shanmugavel\Book1.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
object[] array1 = new object[100];
int k = 0;
for (rCnt = 1; rCnt <= rw; rCnt++)
{
for (cCnt = 1; cCnt <= cl; cCnt++)
{
array1[k] = (range.Cells[rCnt, cCnt] as Excel.Range).Value2;
if (array1[k]==null)
{
array1[k] = null;
}
k++;
}
try
{
string oradb = "Provider=**;Data Source=**;User Id=**;Password=**;";
string cmdText = @"INSERT INTO table_name
(SESSION_ID,TERRITORY_ID,SCANNER_SERIAL_NUM,RESPONDENT_ID,HOUSEHOLD_ID,QUESTIONAIRE_ID,SECTION_ID,QUESTION_ID,TEMPLATE_ID,DETAILS_ID,DATA,TIME_STAMP,INSERTED_DATE)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
OleDbConnection con = new OleDbConnection(oradb);
con.Open();
OleDbCommand cmd = new OleDbCommand(cmdText, con);
cmd.Parameters.Add("p1", OleDbType.VarChar).Value = array1[0];
cmd.Parameters.Add("p2", OleDbType.VarChar).Value = array1[1];
cmd.Parameters.Add("p3", OleDbType.VarChar).Value = array1[2];
cmd.Parameters.Add("p4", OleDbType.VarChar).Value = array1[3];
cmd.Parameters.Add("p5", OleDbType.VarChar).Value = array1[4];
cmd.Parameters.Add("p6", OleDbType.VarChar).Value = array1[5];
cmd.Parameters.Add("p7", OleDbType.VarChar).Value = array1[6];
cmd.Parameters.Add("p8", OleDbType.VarChar).Value = array1[7];
cmd.Parameters.Add("p9", OleDbType.VarChar).Value = array1[8];
cmd.Parameters.Add("p10", OleDbType.VarChar).Value = array1[9];
cmd.Parameters.Add("p11", OleDbType.VarChar).Value = array1[10];
cmd.Parameters.Add("p12", OleDbType.VarChar).Value = array1[11];
cmd.Parameters.Add("p13", OleDbType.VarChar).Value = array1[12];
//cmd.Parameters.Add("p14", OleDbType.VarChar).Value = array1[13];
k = 0;
i = cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (i > 0)
{
Response.Write("Succesfull");
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
}
显示错误,例如参数没有任何默认值 如果有人知道,请帮助我。
谢谢。