帐号不存在.帮助我,如果有任何错误的查询.提前thanX [英] Account No does not exist. help me, if any error in query. thanX in advance

查看:97
本文介绍了帐号不存在.帮助我,如果有任何错误的查询.提前thanX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string query = "Select Account_No from Accounts where Account_No='" + txtac1.Text + "-" + txtac2.Text + "'";
                   //MessageBox.Show(query1);
                   da = new SqlDataAdapter(query, cn);
                   dt = new DataTable();
                   da.Fill(dt);


                   if (dt.Rows.Count == 0)
                   {
                       MessageBox.Show("This Account No'" + txtac1.Text + "-" + txtac2.Text + "' does not Exists!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       txtac1.SelectAll();
                       txtac1.Focus();
                   }
                   else
                   {

                       Account_Search accountsearch = new Account_Search();
                       accountsearch.Show();
                   }

推荐答案

错误内容取决于您要运行的查询.
按原样的查询将查找一个帐号,其中Account_No列的文本与文本"xxx-yyy"匹配,其中"xxx"是txtac1的内容,而"yyy"是txtac2的内容.它不会查找与txtac1的数字valke减去txtac2的数字值匹配的帐号.如果您打算这样做,那么在将值传递给SQL之前,您需要删除引号,或者执行转换和减法操作.

无论如何,您都不应该那样做:不要串联字符串来构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.代替使用参数化查询.
What the error is depends on the query you intended to run.
The query as it stands will look for an account number where the text of the Account_No column matches the text "xxx-yyy" where "xxx" is the content of txtac1 and "yyy" is the content of txtac2. It will not look for an account number that matches the numeric valke of txtac1 minus the numeric value of txtac2. If that is what you intend, then you need to either remove teh quote marks, or perform a conversion and subtraction before you hand the value through to SQL.

You should not do it that way anyway: Do not 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. Use Parametrized queries instead.


您的代码完全错误.....由于无法向数据表对象"dt"分配值,因此无法使用.

检查此链接,以获取有关从数据库入门指南到数据库的数据访问的想法.通过C#访问SQL Server [ ^ ]
Your code is totally wrong.....It will not work since the datatable object ''dt'' is never assigned a value.

Check this link for the idea about accessing data from database Beginners guide to accessing SQL Server through C#[^]


这篇关于帐号不存在.帮助我,如果有任何错误的查询.提前thanX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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