将xml加载到数据库(变量范围)(load xml into a database(variable scope))

我有一个编程问题,我可以成功加载数据库(访问)与xml,当我把它变成一个函数它(tabelName)在当前上下文中不存在。 如何正确引用它? 我将在数据库中包含多个包含信息的表格。

private void loadDatbase(String fileName, String tabelName) { { { try { string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\set.mdb;Persist Security Info=False"); using (OleDbConnection Conn = new OleDbConnection(ConnString)) { SetCon.Text = "In try"; Conn.Open(); DataSet ds = new DataSet(); ds.ReadXml(@"c:\\temp\\"+fileName+".xml"); OleDbCommand cmd = new OleDbCommand(); DataTable dtCSV = new DataTable(); dtCSV = ds.Tables[0]; cmd.Connection = Conn; cmd.CommandType = CommandType.Text; for (int row = 0; row <= dtCSV.Rows.Count - 1; row++) { cmd.Parameters.Clear(); if (dtCSV.Columns.Count > 1) { cmd.Parameters.Add(new OleDbParameter("@Property", (dtCSV.Rows[row][0]))); cmd.Parameters.Add(new OleDbParameter("@Pvalue", (dtCSV.Rows[row][1]))); cmd.Parameters.Add(new OleDbParameter("@Pdefault", (dtCSV.Rows[row][2]))); cmd.Parameters.Add(new OleDbParameter("@PType", (dtCSV.Rows[row][3]))); //cmd.CommandText = ("INSERT INTO table1 " (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); //The above works no wories however the string below does not tableName does not exist in the current context cmd.CommandText = ("INSERT INTO "+tableName+" (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); cmd.ExecuteNonQuery(); } } } } catch (Exception ex) { richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "" + "\n"; ; } } } }

I have a programming problem I can load a database(access) with xml successfully, when I turn it into a function it(tabelName) does not exist in the current context. How to reference it properly? I will have multiple tabels in the database containing information.

private void loadDatbase(String fileName, String tabelName) { { { try { string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\set.mdb;Persist Security Info=False"); using (OleDbConnection Conn = new OleDbConnection(ConnString)) { SetCon.Text = "In try"; Conn.Open(); DataSet ds = new DataSet(); ds.ReadXml(@"c:\\temp\\"+fileName+".xml"); OleDbCommand cmd = new OleDbCommand(); DataTable dtCSV = new DataTable(); dtCSV = ds.Tables[0]; cmd.Connection = Conn; cmd.CommandType = CommandType.Text; for (int row = 0; row <= dtCSV.Rows.Count - 1; row++) { cmd.Parameters.Clear(); if (dtCSV.Columns.Count > 1) { cmd.Parameters.Add(new OleDbParameter("@Property", (dtCSV.Rows[row][0]))); cmd.Parameters.Add(new OleDbParameter("@Pvalue", (dtCSV.Rows[row][1]))); cmd.Parameters.Add(new OleDbParameter("@Pdefault", (dtCSV.Rows[row][2]))); cmd.Parameters.Add(new OleDbParameter("@PType", (dtCSV.Rows[row][3]))); //cmd.CommandText = ("INSERT INTO table1 " (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); //The above works no wories however the string below does not tableName does not exist in the current context cmd.CommandText = ("INSERT INTO "+tableName+" (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); cmd.ExecuteNonQuery(); } } } } catch (Exception ex) { richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "" + "\n"; ; } } } }

最满意答案

您的方法参数名称称为tabelName ,但您使用的是变量tableName - 请注意拼写。 更改方法: private void loadDatbase(String fileName, String tableName)

Your method parameter name is called tabelName, BUT you are using the variable tableName -- notice the spelling. Change your method: private void loadDatbase(String fileName, String tableName)

将xml加载到数据库(变量范围)(load xml into a database(variable scope))

我有一个编程问题,我可以成功加载数据库(访问)与xml,当我把它变成一个函数它(tabelName)在当前上下文中不存在。 如何正确引用它? 我将在数据库中包含多个包含信息的表格。

private void loadDatbase(String fileName, String tabelName) { { { try { string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\set.mdb;Persist Security Info=False"); using (OleDbConnection Conn = new OleDbConnection(ConnString)) { SetCon.Text = "In try"; Conn.Open(); DataSet ds = new DataSet(); ds.ReadXml(@"c:\\temp\\"+fileName+".xml"); OleDbCommand cmd = new OleDbCommand(); DataTable dtCSV = new DataTable(); dtCSV = ds.Tables[0]; cmd.Connection = Conn; cmd.CommandType = CommandType.Text; for (int row = 0; row <= dtCSV.Rows.Count - 1; row++) { cmd.Parameters.Clear(); if (dtCSV.Columns.Count > 1) { cmd.Parameters.Add(new OleDbParameter("@Property", (dtCSV.Rows[row][0]))); cmd.Parameters.Add(new OleDbParameter("@Pvalue", (dtCSV.Rows[row][1]))); cmd.Parameters.Add(new OleDbParameter("@Pdefault", (dtCSV.Rows[row][2]))); cmd.Parameters.Add(new OleDbParameter("@PType", (dtCSV.Rows[row][3]))); //cmd.CommandText = ("INSERT INTO table1 " (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); //The above works no wories however the string below does not tableName does not exist in the current context cmd.CommandText = ("INSERT INTO "+tableName+" (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); cmd.ExecuteNonQuery(); } } } } catch (Exception ex) { richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "" + "\n"; ; } } } }

I have a programming problem I can load a database(access) with xml successfully, when I turn it into a function it(tabelName) does not exist in the current context. How to reference it properly? I will have multiple tabels in the database containing information.

private void loadDatbase(String fileName, String tabelName) { { { try { string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\set.mdb;Persist Security Info=False"); using (OleDbConnection Conn = new OleDbConnection(ConnString)) { SetCon.Text = "In try"; Conn.Open(); DataSet ds = new DataSet(); ds.ReadXml(@"c:\\temp\\"+fileName+".xml"); OleDbCommand cmd = new OleDbCommand(); DataTable dtCSV = new DataTable(); dtCSV = ds.Tables[0]; cmd.Connection = Conn; cmd.CommandType = CommandType.Text; for (int row = 0; row <= dtCSV.Rows.Count - 1; row++) { cmd.Parameters.Clear(); if (dtCSV.Columns.Count > 1) { cmd.Parameters.Add(new OleDbParameter("@Property", (dtCSV.Rows[row][0]))); cmd.Parameters.Add(new OleDbParameter("@Pvalue", (dtCSV.Rows[row][1]))); cmd.Parameters.Add(new OleDbParameter("@Pdefault", (dtCSV.Rows[row][2]))); cmd.Parameters.Add(new OleDbParameter("@PType", (dtCSV.Rows[row][3]))); //cmd.CommandText = ("INSERT INTO table1 " (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); //The above works no wories however the string below does not tableName does not exist in the current context cmd.CommandText = ("INSERT INTO "+tableName+" (Property, Pvalue, Pdefault, PType) VALUES (? , ?, ?, ?)"); cmd.ExecuteNonQuery(); } } } } catch (Exception ex) { richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "" + "\n"; ; } } } }

最满意答案

您的方法参数名称称为tabelName ,但您使用的是变量tableName - 请注意拼写。 更改方法: private void loadDatbase(String fileName, String tableName)

Your method parameter name is called tabelName, BUT you are using the variable tableName -- notice the spelling. Change your method: private void loadDatbase(String fileName, String tableName)