如何通过编码从sql数据库获取数据到gridview? [英] how to get data from sql database to gridview by coding ?

查看:105
本文介绍了如何通过编码从sql数据库获取数据到gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过编码从数据库获取数据到gridview ...........



我的列是id,date_time和viewbooking



在视图预订中我创建了超链接以显示来自其他表单的数据。

how to get data from database to gridview by coding...........

my column are id, date_time and viewbooking

as in view booking i have created hyperlink to show data from another form.

推荐答案

您好


试试这个代码



Hi

try this code

try{

               SqlConnection con = new SqlConnection("");//connection name

               con.Open();

               SqlCommand cmd = new SqlCommand("select * from tablename", con);

               cmd.CommandType = CommandType.Text;

               SqlDataAdapter da = new SqlDataAdapter(cmd);

               DataSet ds = new DataSet();

               da.Fill(ds, "ss");

               dataGridView1.DataSource = ds.Tables["ss"]; ;

              // dataGridView1.DataBind();

            }

            catch

            {

              MessageBox.Show("No Record Found");

            }


参见下面的代码,

1.将数据库中的记录读取到datareader

2.然后将datareader结果分配给gridview



在这个例子中我使用Mysql来演示。如果您使用的是SQL Server。除了你需要使用各自的类之外没有太大的不同。

See below code,
1. Read the records from the database to a datareader
2. then assign the datareader results to the gridview

In this example I am using Mysql to demonstrate. If you are using SQL Server. there is no much different except the you need to use respective classes.
 private void GetResults()
        {
            //Establishing the MySQL Connection
             MySqlConnection conn = new MySqlConnection("Database=potentiality_live;Data Source=eu;User Id=ptly;Password=phat40");

            string query;
            MySqlCommand SqlCommand;
            MySqlDataReader reader;

            MySqlDataAdapter adapter = new MySqlDataAdapter();
//Open the connection to db
            conn.Open();

//Generating the query to fetch the contact details
            query = "SELECT id,date_time,link FROM'sdfsdfsdf'";

 SqlCommand = new MySqlCommand(query, conn);
            adapter.SelectCommand = new MySqlCommand(query, conn);
//execute the query
            reader = SqlCommand.ExecuteReader();
//Assign the results 
            GridView1.DataSource = reader;

//Bind the data
            GridView1.DataBind();

}





如果您有任何其他问题,请随时问我。如果此解决方案有效,请点击接受解决方案。



谢谢



Feel free to ask me if you have any further issues. If this solution is working then please click on the accept solution.

thank you




查看此示例

Hi ,
Check this example
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }

}
DataTable GetData()
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testconection"].ConnectionString))
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("select id , name from tableName ",con))
        {

            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            adpt.Fill(dt);
        }

    }
    return dt;
}



最好的问候

M.Mitwalli


Best Regards
M.Mitwalli


这篇关于如何通过编码从sql数据库获取数据到gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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