没有从数据库中选择的数据 [英] nothing data selected from the database

查看:82
本文介绍了没有从数据库中选择的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



i希望在文本框区域中选择数据库中的数据但是它没有选择并向我显示我的异常我的代码有什么问题请告诉我我。我想用enter事件创建它以便如何实现。





im使用此代码。







 私人  void  txtPriceGold_Enter( object  sender,EventArgs e)
{
string connstr = @ Server =。\ SQLEXPRESS; Initial Catalog = ABJDATABASE; Integrated Security =真;最大池大小= 100;
string strSql = 从PriceTable中选择txtPriceGold其中txtPriceGold = @txtPriceGold;
SqlConnection conn = new SqlConnection(connstr);

尝试
{
conn.Open();
SqlCommand objCommand = new SqlCommand(strSql,conn);
使用(SqlDataReader dr = objCommand.ExecuteReader())
{
bool success = dr.Read();
if (成功)
{
txtPriceGold.Text = dr.GetString( 0 );
}
}

conn.Close();
}
catch (例外)
{
MessageBox.Show( 错误);
}
}

解决方案

将此行更改为

  string  strSql =  从PriceTable中选择txtPriceGold其中txtPriceGold = @txtPriceGold; 





  string  strSql =  从PriceTable中选择txtPriceGold其中txtPriceGold =' + txtPriceGold +  '; 





如果你想使用@txtPriceGold,那么传递参数的值。


hi
用这个替换你的查询

< pre lang =c#> string strSql = 选择来自PriceTable的txtPriceGold其中txtPriceGold = + txtPrice Gold + ;



if txtPriceGold是你的文本框字段然后

<前lang =c#> 字符串 strSql = 从PriceTable中选择txtPriceGold其中txtPriceGold = + txtPriceGold.Text + ;



请记住:

case1 :当您的数据类型为字符串或varchar2

我们需要通过'+ +'传递值,即单引号,然后双引号然后+

例如

  从Customer中选择名称name =' + name +  '; 





case2 :当你的数据类型是数字或整数时

我们需要传递+ +的值

  string  strSql =  从PriceTable中选择txtPriceGold其中txtPriceGold = + txtPriceGold +  ; 









希望这有助于

快乐编码:)


Dear All,

i want to select the data from database in a textbox area but its nothing select and show me my exception what is wrong in my code please tell me. i want to creates it with enter event so how it is possible.


i m using this code.



  private void txtPriceGold_Enter(object sender, EventArgs e)
        {
            string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=ABJDATABASE;Integrated Security=True; Max Pool Size=100";
            string strSql = "select txtPriceGold from PriceTable Where txtPriceGold=@txtPriceGold";
            SqlConnection conn = new SqlConnection(connstr);

            try
            {
                conn.Open();
                SqlCommand objCommand = new SqlCommand(strSql, conn);
                using (SqlDataReader dr = objCommand.ExecuteReader())
                {
                    bool success = dr.Read();
                    if (success)
                    {
                        txtPriceGold.Text = dr.GetString(0);
                    }
                }

                conn.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("error");
            }
}

解决方案

Change this line to

string strSql = "select txtPriceGold from PriceTable Where txtPriceGold=@txtPriceGold";



string strSql = "select txtPriceGold from PriceTable Where txtPriceGold='"+txtPriceGold +"'";



If you want to use @txtPriceGold then pass the value for the parameter.


hi Replace your Query with this

string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+txtPriceGold +"";


if txtPriceGold is your Textbox Field then

string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+txtPriceGold.Text+"";


Remember:
case1:when your datatype is string or varchar2
we need to pass the value with '"+ +"'i.e single Quote,then Double Quote and then +
for example

"select name from Customer Where name ='"+name+"'";



case2 :when your datatype is numeric or integer
we need to pass the value with "+ +"

string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+txtPriceGold +"";





Hope this helped
Happy Coding:)


这篇关于没有从数据库中选择的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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