【发布时间】:2016-12-16 23:53:56
【问题描述】:
我的 dbconnect 类中有这个主要的插入函数。我想使用这个功能的所有形式。我调用了函数,但它不起作用。我能做些什么 ?我的错在哪里?
这是我的 DBConnect 课程
public bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
public bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//Insert statement
public void Insert(string tablename , ArrayList [] values )
{
string val = "VALUES" + "(" ;
for (int i=0; i<values.Length; i++ )
{
if (values.Length > 1)
val += values[i] + ",";
else val += values[i];
}
val += ")";
string query = "INSERT INTO "+ tablename + val ;
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
这是单击按钮时的调用函数。我想所以我没有找到什么会来这里“????”
private void button1_Click(object sender, EventArgs e)
{
DBConnect conn = new DBConnect();
conn.Insert("rezervationinformations", *????* );
}
【问题讨论】:
-
包含要插入的值的数组。
-
您还需要将
"VALUES" + "(" ;更改为" VALUES" + "(" ;,以便它们是您的表名和关键字VALUES之间的空格。 -
我知道我有语法错误,但我需要按钮单击上的函数参数
标签: c# arrays winforms arraylist sql-insert