从asp.c#将数据插入MySql表中 [英] Insert data in MySql table from asp.c#

查看:71
本文介绍了从asp.c#将数据插入MySql表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不起作用,您能告诉我原因吗?

This is not working can you tell me the reason please

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data;
namespace web_connection
{
    public partial class _Default : System.Web.UI.Page
    {  
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string MyConString = "SERVER=localhost;DATABASE=sale;UID=root;PASSWORD=alimola;";
			MySqlConnection connection = new MySqlConnection(MyConString);
			string cmdText = "INSERT INTO customer(C_Name ,E-mail ,Password ,Address)VALUES (''C_Name.Text'',''E-mail.Text'',''Password.Text'',''Address.Text'')";
                MySqlCommand cmd = new MySqlCommand(cmdText, connection);
 
                connection.Open();
 
                int result = cmd.ExecuteNonQuery();
                connection.Close();
 
                //lblError.Text = "Data Saved";
 
            }
            catch (Exception )
            {
               Console.Write("not entered");
                //lblError.Text = ex.Message;
            }
        }
    }
}



[edit]主题,标签,删除txtspeak,添加代码块,整理缩排,删除空行-OriginalGriff [/edit]



[edit]Subject, Tags, txtspeak removed, code block added, indentation tidied, blank lines removed - OriginalGriff[/edit]

推荐答案

首先,您不应该在这里输入密码.这是一个全球站点,在这里输入密码绝对不是最好的选择.
其次,在执行插入查询之前,请确保您能够连接到数据库并且此用户名/密码组合正确无误.
First of all, you should not be putting in a password here. This is a global site and putting a password here is definitely not the best thing.
Secondly, make sure you are able to connect to the database and that this username / password combination is correct before you execute the insert query.


更改为此:


字符串cmdText =插入客户(C_Name,E-mail,Password,Address)VALUES(""+ C_Name.Text +"''," + E-mail.Text +'',""+ Password.文字+'',''" ++ Address.Text +'')";


如果可行,请告诉我....
change to this :


string cmdText = "INSERT INTO customer(C_Name ,E-mail ,Password ,Address)VALUES (''"+ C_Name.Text+"'',''"+E-mail.Text+"'',''"+Password.Text+"'',''"+Address.Text+"'')";


if it works let me know....


首先,永远不要使用字符串连接来构建SQl查询,而应该使用命令参数.这样,您将始终确保将正确的数据类型传递给数据库.建立查询时发现错误也将容易得多.在您的情况下,我会这样做

First off, you should never use string concatenation to build an SQl Query, instead you should use command parameters. That way you will always ensure that the correct data types are being passed on to the database. It will also be much easier to spot mistakes when building the query. In your case I would do this

protected void Button1_Click(object sender, EventArgs e)
{
    string MyConString = "SERVER=localhost;DATABASE=sale;UID=root;PASSWORD=alimola;";
    using (MySqlConnection connection = new MySqlConnection(MyConString))
    {
        try
        {
            string cmdText = "INSERT INTO customer(C_Name, E-mail, Password, Address) VALUES (@C_Name, @E-mail, @Password, @Address)";
            MySqlCommand cmd = new MySqlCommand(cmdText, connection);
            cmd.Parameters.AddWithValue("@C_name", c_Name.Text);
            cmd.Parameters.AddWithValue("@E-mail", E - mail.Text);
            cmd.Parameters.AddWithValue("@Password", Password.Text);
            cmd.Parameters.AddWithValue("@Address", Address.Text);
            connection.Open();
            int result = cmd.ExecuteNonQuery();
            //lblError.Text = "Data Saved";
        }
        catch (Exception)
        {
            MessageBox.Show("not entered");
            //lblError.Text = ex.Message;
        }
    }
}



还要注意,我使用using语句创建了连接.这确保了连接对象将始终被丢弃.这也意味着我不需要特别关闭连接.

正如所有人所说,请勿在代码中使用密码.使用web.config文件保存连接字符串,然后从那里访问字符串

希望对您有帮助



Notice also that I created my connection with a using statement. This ensures that the connection object will always be diposed of come what may. this also means that I do not specifically need to close the connection.

And as everyone says DO NOT USE PASSWORDS IN CODE. Use the web.config file to save connection stings, and access the string from there

Hope this helps


这篇关于从asp.c#将数据插入MySql表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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