插入数据时出错 [英] Error inserting the Data

查看:88
本文介绍了插入数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string query = "Insert into survey(name,mobile,email,use,company,a1,a2,a3,a4,a5,a6)values('" + name.Text + "','" + mobile.Text + "','" + email.Text + "','"+Dropdownlist1.SelectedItem.Value+"','"+cmptxt.Text+"','" + a1.SelectedItem.Text + "','" + a2.SelectedItem.Text + "','" + a3.SelectedItem.Text + "','" + a4.SelectedItem.Text + "','" + a5.SelectedItem.Text + "','" + a6.Text + "')";
       MySqlCommand cmd = new MySqlCommand(query, con);







我得到的错误将数据插入数据库是






The error i got inserting the data in database is

MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'use,company,a1,a2,a3,a4,a5,a6)values('eer','3432','jhjkahjkasd.com','Commercial'' at line 1 at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at _Default.Submit_Click(Object sender, EventArgs e) in e:\Important\Software Product Survey\Default.aspx.cs:line 25 

推荐答案

USE 是一个关键字,你需要用一个(`)来放置MySql引号:

USE is a keyword, you need to put MySql quotes around it with a (`):
string query = "Insert into survey(name,mobile,email,`use`,company ...


应该很容易将断点放在MySqlCommand cmd = new MySqlCommand(query,con);.复制查询变量内容并直接在数据库上运行。
Its should be easy to put the break point at the line MySqlCommand cmd = new MySqlCommand(query, con);. Copy the query variable contents and run directly on the database.


更改下面的SQL和下面的代码接受参数



change your SQL as below and the code as below to accept parameters

string query ="Insert into survey(`name`,`mobile`,`email`,`use`,`company`,a1,a2,a3,a4,a5,a6)values(@name,@mobile,@email,@use,@company,@a1,@a2,@a3,@a4,@a5,@a6)";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@name", name.Text);
// add all the parameter values


这篇关于插入数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆