')'附近的语法不正确(sql exception) [英] incorrect syntax near ')' (sql exception)

查看:74
本文介绍了')'附近的语法不正确(sql exception)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UGIcon.Open();
                    cmd = new SqlCommand("update admin set name='" + textBox5.Text + "', age='" + numericUpDown1.Value.ToString() + "', phone='" + numericUpDown2.Value.ToString() + "', question='" + comboBox1.SelectedItem.ToString()+"', answer='" + textBox4.Text + "')", UGIcon);
                    
                       SqlDataReader sd;
                    sd = cmd.ExecuteReader(); i m getting error here 

                    MessageBox.Show("Thank You!!! Your Details updated", "Updation Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    groupBox1.Visible = false;
                    UGIcon.Close();

推荐答案

mylib.cmd = new SqlCommand("Update BUHeadMstr set Eid =@Eid, Bid=@Bid,CorpId=@CorpId,CreatedBy=@UId,CreatedOn=@Date where Buheadid =@buheadId    ", mylib.objDBConn);
            mylib.cmd.CommandType = CommandType.Text;



mylib.cmd.Parameters.AddWithValue (@ Eid,objAdmin.Eid);

int i = mylib.cmd.ExecuteNonQuery();


mylib.cmd.Parameters.AddWithValue("@Eid", objAdmin.Eid);
int i = mylib.cmd.ExecuteNonQuery();


在编写内联SQL时请使用参数化查询和你一样,下面是一个如何做到这一点的例子。



C#SqlParameter示例 [ ^ ]



使用这种方法减少/消除SQL注入的机会(在下面的文章中描述)



参数化查询如何帮助SQL注入 [ ^ ]



此代码项目文章重点介绍了使用从数据库插入,更新和删除数据的方法ADO.NET 。正如你所看到的那样。它使用 executeNonQuery ,因为它返回了您的查询所影响的行数。



使用C#进行简单的ADO.NET数据库读取,插入,更新和删除。 [ ^ ]
Please use paramterised queries when writing inline SQL such as yours, below is an example of how to do this.

C# SqlParameter Example[^]

using this approach reduces / removes the chance of SQL Injection (decribed in the following article)

How do Parameterised queries help against SQL Injection[^]

This code project article highlights the ways to insert, update and delete data from a database using ADO.NET. As you can see when you read it. It uses the executeNonQuery as this returns the number of rows that have been effected by your query.

Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]


您好,



为什么使用ExecuteReader()代替使用cmd .ExecuteNonQuery()方法。
Hello,

Why u r using ExecuteReader() instead of use cmd.ExecuteNonQuery() method.


这篇关于')'附近的语法不正确(sql exception)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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