错误更新性别 [英] Error update gender

查看:60
本文介绍了错误更新性别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gender ='+ a +'

不更新工作请回答



我尝试了什么:



string a;

private void button1_Click(object sender,EventArgs e)

{

if(radiomal.Checked)

{

a = radiomal.Text;

}

else

{

a = radiofem.Text;

}





SqlConnection abc = new SqlConnection(Data Source = DESKTOP-8BEJFRN \\ SQLEXPRESS; Initial Catalog = assignment1; Integrated Security = True);

abc.Open( );

SqlCommand bcd = new SqlCommand(update cust set [Nic No] ='+ txtnicn1.Text +',Name ='+ txtnam1.Text +',Address =' + txtadd.Text +',[Mobile no] ='+ txtcon1.Text +',gender ='+ a +'where [Custermer Id] ='+ txtcus id1.Text +',abc);

bcd.ExecuteNonQuery();

MessageBox.Show(你的更新);

错误更新性别请帮助

gender ='"+a+"'
not update work pls answer

What I have tried:

string a;
private void button1_Click(object sender, EventArgs e)
{
if (radiomal.Checked)
{
a = radiomal.Text;
}
else
{
a = radiofem.Text;
}


SqlConnection abc = new SqlConnection("Data Source=DESKTOP-8BEJFRN\\SQLEXPRESS;Initial Catalog=assignment1;Integrated Security=True");
abc.Open();
SqlCommand bcd = new SqlCommand("update cust set [Nic No]='" + txtnicn1.Text + "',Name='" + txtnam1.Text + "',Address='" + txtadd.Text + "',[Mobile no]='" + txtcon1.Text + "',gender='"+a+"' where [Custermer Id] ='" + txtcusid1.Text + "'", abc);
bcd.ExecuteNonQuery();
MessageBox.Show("your update ");
error update gender pls help

推荐答案

不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



修复整个你的应用程序 - 如果你错过了一个,其他人不会 - 并且你有问题注意到会同时消失。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your app - and if you miss one, somebody else won't - and the problem you have noticed will vanish at the same time.


1。获得帮助的最佳方式是使用最佳实践。除了安全隐患之外,它还使这个实例更容易阅读。



2.你没有发布实际表格,所以我们不知道是什么你继续接收收音机盒



3.尝试调试并查看分配给 a 的值。



这是您的代码应该的粗略草图:

1. The best way to get help is to use best-practices. Besides the security implications, it makes it a lot easier to read in this instance.

2. You didn't post the actual form so we can't tell what you got going on for the radio boxes

3. Try debugging and see what value is assigned to a.

This is a rough draft of what your code should look like:
private void button1_Click(object sender, EventArgs e) {

    int RowsAffected = -1;
    if (radiomal.Checked) { a = radiomal.Text; }
    else { a = radiofem.Text; }

    string ConnInfo = "Data Source=;Initial Catalog=;Integrated Security=True";
    string CmdText = "UPDATE cust   SET [Nic No] = @NicNo, Name = @Name, Address = @Address, [Mobile no] = @MobileNo, gender = @Gender  WHERE [Custermer Id] = @CustomerID";
    using (SqlConnection conn = new SqlConnection(ConnInfo)) {
        SqlCommand cmd = new SqlCommand(CmdText, conn);

        cmd.Parameters.AddWithValue("@NicNo", txtnicn1.Text);
        cmd.Parameters.AddWithValue("@Name", txtnam1.Text);
        cmd.Parameters.AddWithValue("@Address", txtadd.Text);
        cmd.Parameters.AddWithValue("@MobileNo", txtcon1.Text);
        cmd.Parameters.AddWithValue("@Gender", a);
        cmd.Parameters.AddWithValue("@CustomerID", txtcusid1.Text);

        conn.Open();
        RowsAffected = cmd.ExecuteNonQuery();
        conn.Close();
    }
    MessageBox.Show(string.Format ("Your update affected {0} rows", RowsAffected));
}


这篇关于错误更新性别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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