靠近'的语法不正确;用户名';-错误c#

靠近'的语法不正确;用户名';-错误c#,c#,sql,C#,Sql,我尝试了所有的方法,搜索了相关的主题,但没有任何效果 这是我的密码 { byte[] images = null; FileStream fs = new FileStream(imgLocation, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs);

我尝试了所有的方法,搜索了相关的主题,但没有任何效果

这是我的密码

            {
                byte[] images = null;
                FileStream fs = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                images = br.ReadBytes((int)fs.Length);

                SqlConnection con2 = new SqlConnection(@"Data Source=USER-PC; Initial Catalog=ProfRegistration; Integrated Security=True;");
                con2.Open();
                String str2 = "INSERT INTO ProfInfo(UserID,FirstName,MiddleName,LastName,Department,Username,Password,Image)  VALUES('" + txtID.Text + "' , '" + txtFirstName.Text + "' , '" + txtMiddleName.Text + "' , '" + txtLastName.Text + "', '" + txtDep.Text + "' '" + txtUsername.Text + "', '" + txtPassword.Text + "', @Image)";
                SqlCommand cmd2 = new SqlCommand(str2, con2);
                cmd2.Parameters.Add(new SqlParameter("@UserID", txtID.Text));
                cmd2.Parameters.Add(new SqlParameter("@FirstName", txtFirstName.Text));
                cmd2.Parameters.Add(new SqlParameter("@MiddleName", txtMiddleName.Text));
                cmd2.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
                cmd2.Parameters.Add(new SqlParameter("@Department", txtDep.Text));
                cmd2.Parameters.Add(new SqlParameter("@Username", txtUsername.Text));
                cmd2.Parameters.Add(new SqlParameter("@Password", txtPassword.Text));
                cmd2.Parameters.Add(new SqlParameter("@Image", images));
                cmd2.ExecuteNonQuery();
                MessageBox.Show("User has been added!");
                Login frmLogin = new Login();
                frmLogin.Show();
            }  

我已经使用了cmd.Parameters.Add(……而不是cmd.Parameters.AddWithValue(……因为它提供了更多的错误和异常……

错误的原因是语法错误,但是语法错误只是问题的一部分

你的代码对你来说是完全开放的

使用参数化查询的正确方法是在查询中包含参数。这将使调试更加容易,因为您不会有上千个串联字符串:

string query = "INSERT INTO ProfInfo(UserID,FirstName,MiddleName,LastName,Department,Username,Password,Image)  VALUES(@UserID, @FirstName, @MiddleName, @LastName, @Department, @Username, @Password, @Image)";

SqlCommand cmd2 = new SqlCommand(query , con2);
cmd2.Parameters.Add(new SqlParameter("@UserID", txtID.Text));            
cmd2.Parameters.Add(new SqlParameter("@FirstName", txtFirstName.Text));
cmd2.Parameters.Add(new SqlParameter("@MiddleName", txtMiddleName.Text));
cmd2.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
cmd2.Parameters.Add(new SqlParameter("@Department", txtDep.Text));
cmd2.Parameters.Add(new SqlParameter("@Username", txtUsername.Text));
cmd2.Parameters.Add(new SqlParameter("@Password", txtPassword.Text));
cmd2.Parameters.Add(new SqlParameter("@Image", images));
然而,这并不是你所有的问题

除了大量SQL注入漏洞之外,您还将密码存储为纯文本。这是一个可怕的想法。在存储之前,您需要对密码进行加密和哈希处理


我强烈建议花一些时间研究一些基本的安全主题,如SQL注入和密码盐析与哈希。有大量可用的免费资源。

您的代码中的基本问题是您在SQL中遗漏了逗号

+ "' '" + txtUsername.Text + "', '
使用

+ "', '" + txtUsername.Text + "', '
Rest您的代码需要修复,如其他答案中所述。如SQL注入


另外,您正在
sqlcommand
对象中使用参数,但它们不在sql查询中。在查询中添加它们

如果您已经在使用sql参数,为什么要连接sql?您非常容易受到sql注入攻击…如果您以正确的方式使用sql参数,则不会出现错误。密码在大多数情况下是保留字SQL方言。也永远不要将密码存储为纯文本-哈希和盐。任何形式的Add/AddWithValue都会导致错误,因为您的SQL没有任何参数(但应该有)。而且您不需要二进制读取器来获取映像-
文件。ReadAllBytes()
将执行sweel.LMFAO.。它为什么不在SQL数据库中写入“用户已添加”。。然后我回头看表,它并没有出现(我已经刷新了它)。