C#连接数据库和安全输入数据

时间:2017-02-20 20:34:12

标签: c# mysql

我有几个关于在c#中与MySql数据库连接的问题。

我在youtube https://www.youtube.com/watch?v=IH0dYO8QOow上基于本教程建立了与数据库的连接,但我在视频中评论说这种方法对于MySql注入是有益的。

我在PHP中用MySql中的PDO预处理语句工作,现在我想知道:

  • 最好使用xampp,wamp ....或安装Microfost Sql server在localhost上创建服务器
  • 如何使用c#中的预准备语句进行查询以及如何使其安全

我使用Microsoft Sql server 2014创建数据库。

请不要给我一个负面评分,因为我是c#的新人,想要学习,我听说这是提问和获得适当帮助的最佳场所。

以下是连接数据库并检查有效用户名和密码的简单登录表单代码,如果一切正常,则会打开新窗口并隐藏登录表单。

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // connect to a database
            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Mario\Documents\mojaBaza1.mdf;Integrated Security=True;Connect Timeout=30");

            // make new query
            SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM users WHERE username = '" + textBox1.Text + "' AND password = '" + maskedTextBox1.Text + "'",connection);

            // fetch data from table
            DataTable dt = new DataTable();

            sda.Fill(dt);

            // check query for result, if returned 1 login
            if (dt.Rows[0][0].ToString() == "1")
            {
                // hides new window
                this.Hide();

                // instance of new window when user log in
                Main ss = new Main();

                // show new window
                ss.Show();
            }
            else
            {
                // error message if user entered invalid data
                MessageBox.Show("Invalid username or password, please try again.");
            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Mario\Documents\mojaBaza1.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd= connection.CreateCommand();
cmd.CommandText="SELECT COUNT(*) FROM users WHERE username=@username AND password=@password";
cmd.parameters.AddWithValue("@username",textBox1.Text);
cmd.parameters.AddWithValue("@password",maskedTextBox1.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);

这样您就可以使用查询参数,这几乎就是预编译语句的C#版本。

答案 1 :(得分:0)

使用MySql Wamp。

private void checkuserifexist()
{
MySqlConnection con = new MySqlConnection("SERVER=localhost; user id=root; password=; database=databasename");
con.Open();
try
{
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM login where ID='" + txtid.Text + "'";
MySqlDataReader exist = cmd.ExecuteReader();
if(exist.HasRows)
{
login();
}
else
{
MessageBox.Show("This user doesn't Exist", "ID not exist", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

//login code
private void login()
{
MySqlConnection con = new MySqlConnection("SERVER=localhost; user id=root; password=; database=databasename");
con.Open();
String strusername = txtusername.Text;
String strpassword = txtpassword.Text;

string sql = "SELECT * FROM login WHERE Username='" + strusername + "'AND Password='" + strpassword + "'";
MySqlDataAdapter da = new MySqlDataAdapter(sql, con);
DataTable ds = new DataTable();
da.Fill(ds);

for(int i = 0; dt.Rows.Count; i++)
{
if(dt.Rows[i]["Userlevel"].Equals("Administrator"))
{
this.Hide();
Admin admin = new Admin();
admin.ShowDialog();
}
}
}