好的,所以基本上我已经在PhpMyAdmin上创建了MySQL表。它位于本地主机,用户名root和无密码。
我正在使用基于c#on visual express 2008的Windows应用程序。我有一个按钮来保存/加载来自MySQL的数据(我已经按照一些链接/ tuts来达到这一点,但是dunno这怎么可以理论上连接到MySQL @ phpmyadmin,我的意思是我不需要从PhpmyAdmin数据库下载文件和参考,或者将它作为插件添加到脚本或者什么东西?Tottaly丢失在这里..):
String connString = "SERVER = localhost; DATABASE = request; User ID = root; ID =; UserName =; Date =; Type =; Rules =;";
MySqlConnection mcon = new MySqlConnection(connString);
String command = "SELECT * FROM requesttcw";
MySqlCommand cmd = new MySqlCommand(command, mcon);
MySqlDataReader reader;
try
{
mcon.Open();
cmd.ExecuteNonQuery();
reader = cmd.ExecuteReader();
cmd.CommandType = System.Data.CommandType.Text;
while (reader.Read() != false)
{
Console.WriteLine(reader["ID"]);
Console.WriteLine(reader["ClanName"]);
Console.WriteLine(reader["Date"]);
Console.WriteLine(reader["Type"]);
Console.WriteLine(reader["Rules"]);
}
Console.ReadLine();
}
catch (Exception)
{
MessageBox.Show("ERROR: There was an error trying to connect to the DB!");
return;
}
cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + richTextBox1.Text + "' LIMIT 1)";
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("You're Request Has Been Posted!");
}
catch (Exception ex)
{
string message = ("ERROR: There was an error submitting your form!" + ex + "");
DialogResult result = MessageBox.Show(message, "ERROR", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
switch (result)
{
case DialogResult.Retry:
Application.Restart();
break;
case DialogResult.Cancel:
this.Close();
break;
}
}
当我运行它时,输入我的数据,然后单击按钮,它在线给我这个错误(MySqlConnection mcon = new MySqlConnection(connString); * 关键字不受支持。 参数名称:id *
请告诉我如何将其完全连接到MySQL ..我还下载了MySQL Connector并引用了mysql.data.dll文件。那部分也是这样做的......
答案 0 :(得分:3)
尝试以下连接字符串:
string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
还要尝试清理代码,并确保通过将IDispoable
语句包装在using
资源(例如SQL连接和命令)中来正确处理它们。还要确保使用预准备语句或者您的代码容易受到SQL注入攻击,并且您的数据库可能会很快被破坏(眨眼间):
string connString = "Server=localhost;Database=request;Uid=root;Pwd=;";
using (MySqlConnection mcon = new MySqlConnection(connString))
using (MySqlCommand cmd = mcon.CreateCommand())
{
mcon.Open();
cmd.CommandText = "SELECT * FROM requesttcw";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["ID"]);
Console.WriteLine(reader["ClanName"]);
Console.WriteLine(reader["Date"]);
Console.WriteLine(reader["Type"]);
Console.WriteLine(reader["Rules"]);
}
}
}
using (MySqlConnection mcon = new MySqlConnection(connString))
using (MySqlCommand cmd = mcon.CreateCommand())
{
mcon.Open();
cmd.CommandText = "INSERT INTO requesttcw (ClanName, Date, Type, Rules) VALUES (@ClanName, @Date, @Type, @Rules)";
cmd.Parameters.AddWithValue("@ClanName", textBox1.Text);
// Warning if the Date column in your database is of type DateTime
// you need to parse the value first, like this:
// cmd.Parameters.AddWithValue("@Date", Date.Parse(textBox2.Text));
cmd.Parameters.AddWithValue("@Date", textBox2.Text);
cmd.Parameters.AddWithValue("@Type", textBox3.Text);
cmd.Parameters.AddWithValue("@Rules", richTextBox1.Text);
cmd.ExecuteNonQuery();
}
就异常处理而言,我在我的示例中省略了它,但显然可以将using
语句包装在try / catch块中。
答案 1 :(得分:0)
在您的connString
变量中ID =;
之后User ID = root
。{/ 1>
答案 2 :(得分:0)
您的connectionstring指定应用程序与MySQL服务器之间的连接。 你得到的错误是,因为你在你的连接字符串中犯了一个错误。 我认为以下内容将更好地满足您的需求:
Server=localhost;Database=request;Uid=myUsername;Pwd=myPassword;