我想检索特定行的数据并将其显示在表中 [英] i want to retrieve data of a particular row and display it in a table

查看:71
本文介绍了我想检索特定行的数据并将其显示在表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

  private  DataTable GetData()
{
DataTable dt = new DataTable();
string connection_string = @ Provider = Microsoft .Jet.OLEDB.4.0;数据源= C:\ HILProject\project4 \ App_Data \ adminmin.mdb;
使用(OleDbConnection cn = new OleDbConnection(connection_string))

使用(OleDbDataAdapter adp = new OleDbDataAdapter( 选择姓名,Email_Id,Mob_No,Dob来自UserReg,cn))
使用 (OleDbCommand command = new OleDbCommand())
{
cn.Open();
adp.Fill(dt);
}
return dt;
}
}





i希望使用where username = Textbox1.text检索数据
用户名是表userreg的列名

如何写where子句

解决方案

从以下方面学习:< a href =http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access>参数查询在ASP.NET-with-MS-Access中 [ ^ ]


  string  queryString =  从UserReg中选择Name,Email_Id,Mob_No,Dob,其中username =?; 
使用(OleDbConnection连接=
new OleDbConnection(connectionString))
{
OleDbDataAdapter adapter =
new OleDbDataAdapter(queryString,connection);

// 设置参数。
adapter.SelectCommand。 Parameters.AddWithValue( @ p1,Textbox1.Text);

// 打开连接并填充数据集。
尝试
{
connection.Open();
adapter.Fill(dt);
}
catch (例外情况)
{
// Console.WriteLine(ex.Message);
}
//
// 代码退出使用区块。
}
return dt;





参考: OleDbParameter Class [ ^ ]


my code is as follow:

private DataTable GetData()
    {
        DataTable dt = new DataTable();
        string connection_string = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\HILProject\project4\App_Data\adminlog.mdb";
        using (OleDbConnection cn = new OleDbConnection(connection_string))

        using (OleDbDataAdapter adp = new OleDbDataAdapter("select Name,Email_Id, Mob_No, Dob From UserReg ", cn))
        using (OleDbCommand command = new OleDbCommand()) 
        {
            cn.Open();
          adp.Fill(dt);
        }
        return dt;
    }
}



i want to retrieve data by using "where username = Textbox1.text"
username is a column name of table userreg
how to write where clause

解决方案

Learn to do it from : Parameter-Queries-in-ASP.NET-with-MS-Access[^]


string queryString ="select Name,Email_Id, Mob_No, Dob From UserReg where username =?";
using (OleDbConnection connection =
           new OleDbConnection(connectionString))
{
    OleDbDataAdapter adapter =
        new OleDbDataAdapter(queryString, connection);

    // Set the parameters.
    adapter.SelectCommand.Parameters.AddWithValue("@p1", Textbox1.Text);

    // Open the connection and fill the DataSet.
    try
    {
        connection.Open();
        adapter.Fill(dt);
    }
    catch (Exception ex)
    {
        //Console.WriteLine(ex.Message);
    }
    // The connection is automatically closed when the
    // code exits the using block.
}
return dt;



Reference:OleDbParameter Class[^]


这篇关于我想检索特定行的数据并将其显示在表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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