如何在mysql数据库中插入我的值 [英] how to insert my values in mysql database
问题描述
嗨
我想将我的数据插入Mysql数据库...
下面是我使用的代码
protected void Submit_Click(object sender,EventArgs e)
{
MySqlConnection connection = null;
if(RadioButton1.Checked == true)
{
lblgender.Text = RadioButton1.Text;
}
else
{
lblgender.Text = RadioButton2.Text;
}
lblbusinessCat.Text = DropDownList1.SelectedValue;
using(connection = new MySqlConnection(ConfigurationManager) .ConnectionStrings [MySQLConnectionString]。ConnectionString))
{
试试
{
string sql = INSERT INTO登录(用户名,性别,电子邮件,MobileNo,Category_Id,CompanyArea,CompanyName,Company不,AltCompanyNo,地址)VALUES('@ User'','@ Gender','@ Email','@ MobileNo','@ Category_Id','@ CompanyArea','@ CompanyName','@ CompanyNo','@ AltCompanyNo','@ Address');
//connection.Open();
MySqlCommand cmd = new MySqlCommand(sql);
cmd.Parameters.AddWithValue(@ Username,txtUserName.Text.Trim());
cmd.Parameters.AddWithValue(@ Gender,lblgender。 Text.Trim());
cmd.Parameters.AddWithValue(@ Email,txtEmailId.Text.Trim());
cmd.Parameters.AddWithValue( @MobileNo,txtmobile.Text.Trim());
cmd.Parameters.AddWithValue(@ Category_Id,lblbusinessCat.Text.Trim());
cmd .Parameters.AddWithValue(@ CompanyArea,txtCompanyLoc.Text.Trim());
cmd.Parameters.AddWithValue( @公司名称,txtCompanyname.Text.Trim());
cmd.Parameters.AddWithValue( @ CompanyNo,txtCompNo.Text.Trim()); <无线电通信/>
cmd.Parameters.AddWithValue( @ AltCompanyNo,txtCompaltNo.Text.Trim());
cmd.Parameters.AddWithValue( @地址,txtCompAddress.Text.Trim ());
// MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
cmd.Connection = connection;
connection.Open() ;
cmd.ExecuteNonQuery();
Session [username] = txtUserName.Text;
Response.Redirect(Business_NewUpload.aspx,false);
//lblError.Text =数据保存;
}
catch(例外情况)
{
Response.Write(发生错误:+ ex.Message);
//lblError.Text = ex.Message;
Response.Redirect(BusinessMoney_About.aspx,false);
}
终于
{
if( connection!= null)
{
connection.Close(); //关闭连接
}
}
}
}
这里我的值被存储在数据库中@username,@ gender ...
请any1帮助我...
先谢谢
Hi
I wanted to insert my data's into Mysql database...
Below is the code i used
protected void Submit_Click(object sender, EventArgs e)
{
MySqlConnection connection = null;
if(RadioButton1.Checked==true)
{
lblgender.Text= RadioButton1.Text;
}
else
{
lblgender.Text= RadioButton2.Text;
}
lblbusinessCat.Text=DropDownList1.SelectedValue;
using (connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString))
{
try
{
string sql = "INSERT INTO login(Username, Gender, Email, MobileNo, Category_Id, CompanyArea, CompanyName, CompanyNo, AltCompanyNo, Address)VALUES ('@Username', '@Gender', '@Email', '@MobileNo', '@Category_Id', '@CompanyArea', '@CompanyName', '@CompanyNo', '@AltCompanyNo', '@Address') ";
//connection.Open();
MySqlCommand cmd = new MySqlCommand(sql);
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Gender", lblgender.Text.Trim());
cmd.Parameters.AddWithValue("@Email", txtEmailId.Text.Trim());
cmd.Parameters.AddWithValue("@MobileNo", txtmobile.Text.Trim());
cmd.Parameters.AddWithValue("@Category_Id", lblbusinessCat.Text.Trim());
cmd.Parameters.AddWithValue("@CompanyArea", txtCompanyLoc.Text.Trim());
cmd.Parameters.AddWithValue("@CompanyName", txtCompanyname.Text.Trim());
cmd.Parameters.AddWithValue("@CompanyNo", txtCompNo.Text.Trim());
cmd.Parameters.AddWithValue("@AltCompanyNo", txtCompaltNo.Text.Trim());
cmd.Parameters.AddWithValue("@Address", txtCompAddress.Text.Trim());
//MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
Session["username"] = txtUserName.Text;
Response.Redirect("Business_NewUpload.aspx", false);
//lblError.Text = "Data Saved";
}
catch (Exception ex)
{
Response.Write("An error occured: " + ex.Message);
//lblError.Text = ex.Message;
Response.Redirect("BusinessMoney_About.aspx", false);
}
finally
{
if (connection != null)
{
connection.Close(); //close the connection
}
}
}
}
here my values are getting stored in database as @username, @gender ...
Please any1 help me ...
Thanks in Advance
推荐答案
从值中删除引号,
remove quotes from values,
VALUES (@Username, @Gender, @Email, ...
这篇关于如何在mysql数据库中插入我的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!