我有编程问题我可以成功加载数据库(访问),当我把它变成一个函数时它(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"; ;
}
}
}
}
答案 0 :(得分:1)
您的方法参数名称为tabelName
,但您使用变量tableName
- 请注意拼写。改变你的方法:
private void loadDatbase(String fileName, String tableName)