对象不能是catch中的“InvalidCastException:OleDbParameter”

时间:2017-06-24 13:14:02

标签: c# database visual-studio

我试图做一个简单的任务,从数据库中的一个表“tab2”读取 - >保存到xml然后重新加载到表“tab1”都具有相同的结构,我之前询问过并收到以下内容代码作为答案。

我遇到问题我现在正在为每个项目提供数据库ONCE但是有一个日期时间戳我不认为它正在通过正确读取数据库来编写XML我运行它两次以获取模式和一次数据

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\names.accdb;Persist Security Info=False");
            using (OleDbConnection Conn = new OleDbConnection(ConnString))
            {
                //Conn.Close();//severl times connection has been open
                Conn.Open();
                DataSet ds = new DataSet();
                ds.ReadXml(@"c:\\temp\\tab2.xml");
                OleDbCommand cmd = new OleDbCommand();
                //OleDbCommand cmd1 = new OleDbCommand();
                DataTable dtCSV = new DataTable();
                dtCSV = ds.Tables[0];
                cmd.Connection = Conn;
                cmd.CommandType = CommandType.Text;
                //cmd.Parameters.Add(new OleDbParameter("@field1", Convert.ToString(dtCSV.Rows[0][0])));// = 1234;
                //cmd1.Connection = Conn;

                for (int row = 0; row <= dtCSV.Rows.Count - 1; row++)
                {
                    //for (int col = 0; col < dtCSV.Columns.Count - 1; col++)
                    //{
                    //    //cmd.CommandText = ("INSERT INTO  tab1 ( field1, field2) VALUES (dtCSV.Rows ,dtCSV.Columns)");
                    //}
                    cmd.Parameters.Clear();
                    if (dtCSV.Columns.Count > 1)
                    {
                        //command.Parameters.Add(new OleDbParameter("@EMPID", Convert.Tostring(empsplitIt[1])));
                        //cmd.Parameters.Add(dtCSV.Rows[row][0]);
                        cmd.Parameters.Add(new OleDbParameter("@Field1", Convert.ToString(dtCSV.Rows[row][0])));
                        cmd.Parameters.Add(new OleDbParameter("@dtCSV", Convert.ToString(dtCSV.Rows[row][1])));

                        cmd.CommandText = ("INSERT INTO  tab1 ( field1, field2) VALUES (? , ?)");
                        cmd.ExecuteNonQuery();
                    }

                }

                //Conn.Close();
            }
        }
        catch (Exception ex)
        {
            richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "\n"; ;
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        {
            string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\names.mdb;Persist Security Info=False");

            using (OleDbConnection Conn = new OleDbConnection(ConnString))
            {
                string strSql = "Select * from Table1"; //only launch in main
                richTextBox1.Text = richTextBox1.Text + " Querying Launch Parameters";


                try
                {

                    OleDbConnection con = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\\temp\\names.mdb; Persist Security Info = False");
                    OleDbCommand cmd = new OleDbCommand(strSql, con);
                    con.Open();
                    cmd.CommandType = CommandType.Text;
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "fname,sname");
                    // Extract data set to XML file 
                    ds.WriteXml(@"c:\\temp\\tab2.xml", XmlWriteMode.WriteSchema);
                    ds.WriteXml(@"c:\\temp\\tab2.xml");


                }
                catch (Exception ex)
                {
                    richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "\n"; ;
                }

            }
        }
    }
}

我从我的代码中发现有两个不同的连接字符串,当我放入核心拼写纠正这些时它现在在线上失败

  dtCSV = ds.Tables[0];

用这个

  

错误System.IndexOutOfRangeException:找不到表0。      在System.Data.DataTableCollection.get_Item(Int32索引)      在

的WindowsFormsApp8.Form1.button1_Click(Object sender,EventArgs e)中

代码:

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\names.mdb;Persist Security Info=False");
            using (OleDbConnection Conn = new OleDbConnection(ConnString))
            {
                //Conn.Close();//severl times connection has been open
                Conn.Open();
                DataSet ds = new DataSet();
                ds.ReadXml(@"c:\\temp\\tabel2.xml");
                OleDbCommand cmd = new OleDbCommand();
                //OleDbCommand cmd1 = new OleDbCommand();
                DataTable dtCSV = new DataTable("tabel1");
                dtCSV = ds.Tables[0];
                cmd.Connection = Conn;
                cmd.CommandType = CommandType.Text;
                //cmd.Parameters.Add(new OleDbParameter("@field1", Convert.ToString(dtCSV.Rows[0][0])));// = 1234;
                //cmd1.Connection = Conn;

                for (int row = 0; row <= dtCSV.Rows.Count - 1; row++)
                {
                    //for (int col = 0; col < dtCSV.Columns.Count - 1; col++)
                    //{
                    //    //cmd.CommandText = ("INSERT INTO  tab1 ( field1, field2) VALUES (dtCSV.Rows ,dtCSV.Columns)");
                    //}
                    cmd.Parameters.Clear();
                    if (dtCSV.Columns.Count > 1)
                    {
                        //command.Parameters.Add(new OleDbParameter("@EMPID", Convert.Tostring(empsplitIt[1])));
                        //cmd.Parameters.Add(dtCSV.Rows[row][0]);
                        cmd.Parameters.Add(new OleDbParameter("@field1", Convert.ToString(dtCSV.Rows[row][0])));
                        cmd.Parameters.Add(new OleDbParameter("@field2", Convert.ToString(dtCSV.Rows[row][1])));

                        cmd.CommandText = ("INSERT INTO  tabel1 ( field1, field2) VALUES (? , ?)");
                        cmd.ExecuteNonQuery();
                    }

                }

                //Conn.Close();
            }
        }
        catch (Exception ex)
        {
            richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "\n"; ;
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        {
            string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\names.mdb;Persist Security Info=False");

            using (OleDbConnection Conn = new OleDbConnection(ConnString))
            {
                string strSql = "Select * from tabel2"; //only launch in main
                richTextBox1.Text = richTextBox1.Text + " Querying Launch Parameters";


                try
                {

                    OleDbConnection con = new OleDbConnection(ConnString);
                    OleDbCommand cmd = new OleDbCommand(strSql, con);
                    con.Open();
                    cmd.CommandType = CommandType.Text;
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "fname,sname");
                    // Extract data set to XML file 
                    ds.WriteXml(@"c:\\temp\\tabel2.xml", XmlWriteMode.WriteSchema);
                    ds.WriteXml(@"c:\   \temp\\tabel2.xml");


                }
                catch (Exception ex)
                {
                    richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "\n"; ;
                }

            }
        }
    }
}

0 个答案:

没有答案