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

编程入门 行业动态 更新时间:2024-10-27 22:23:54
本文介绍了如何通过编码从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?

本文发布于:2023-10-20 01:20:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据   sql   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!