从ms SQL中选择并插入datagridview C# [英] Select from ms SQL and insert in datagridview C#

查看:98
本文介绍了从ms SQL中选择并插入datagridview C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮我解决这个问题,我的代码如下:



我尝试了什么:



我试试这个

Help me with this problem, my code is as following:

What I have tried:

I try this

private void prijavaAction() //04.04. final doradjena perfect 
{ 

{ 
SqlConnection con = new SqlConnection(cs); 

if (textBox1.Text.All(char.IsDigit)) 

{ 
string queryString = "select bar_kod, ime,pakovanje, porez_procenat,cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";// pronaci radnika u bazi 
using (SqlConnection connection = new SqlConnection(cs)) 
{ 
SqlCommand command = new SqlCommand(queryString, connection); 
connection.Open(); 
SqlDataReader reader = command.ExecuteReader(); 


if (reader.Read()) 

{ 
//insert into datagrid1 
} 
else 
{ 

MessageBox.Show("Bar kod not exist!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information); 
textBox1.Text = ""; 
} 
} 

}

推荐答案

这是任何数据库的第一件事教程通常会做; 展示如何在网格中呈现表格数据



按照上一个问题,将数据从sql db显示到带有标记列的datagridview [ ^ ],并以相关问题开头,也在本问题的右侧。还有其他一些问题可以回答同样的问题。



这篇快速文章也可以为您提供帮助,演练:在DataGrid控件中显示来自SQL Server数据库的数据Microsoft Docs [ ^ ]



编辑

提供您尝试过的代码以及您所做的评论。您希望确保记录存在,对于数据网格视图并且只渲染其中的记录。

在这种情况下,查询是正确的并且很可能在列中获取数据名称和表和连接是正确的,代码也暴露给SQL注入。我刚刚在这里写了一个答案,使用资源进行Sql注入攻击(.resx )文件 [ ^ ],您的代码很有可能。



文章中已经多次共享 reader.Read()块的代码我在上面的答案中分享的链接。
That is the first thing any database tutorial would normally do; show how to render the table data in a grid.

Follow along the previous question, Display data form sql db onto datagridview with labled columns[^] , and start with the "Related Questions", in the right side of this question as well. There are several other questions that answer the same thing.

This quick article might also help you, Walkthrough: Display Data from a SQL Server Database in a DataGrid Control | Microsoft Docs[^]

Edit
Provided the code that you have tried, as well as the comment that you made. You want to make sure that the record exists, for the data grid view and only render the records it there are any.
In that case, query is correct and will most likely fetch the data if the column names and the table and connection is correct, also the code is exposed to SQL Injection. I just wrote an answer here, Sql injection attacks possible using resource(.resx) file[^] , your code will most likely.

The code for the block of reader.Read() is already shared several times in the articles and the links I have shared in my answer above.


GLOBALVARS.dt = new DataTable();

           if (GLOBALVARS.dt.Columns.Count == 0)
           {
               GLOBALVARS.dt.Columns.Add("bar_kod");
               GLOBALVARS.dt.Columns.Add("ime");
               GLOBALVARS.dt.Columns.Add("cijena_sa_porezom");

           }

  SqlConnection con = new SqlConnection(cs);

           if (textBox1.Text.All(char.IsDigit))

           {
               string queryString = "select bar_kod, ime, cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";


               using (SqlConnection connection = new SqlConnection(cs))
               {
                   SqlCommand command = new SqlCommand(queryString, connection);
                   connection.Open();
                   SqlDataReader reader = command.ExecuteReader();

                   ;
                   while (reader.Read())
                   {
                       if (reader.HasRows)
                       {
                           DataRow dr = GLOBALVARS.dt.NewRow();
                           dr["bar_kod"] = reader["bar_kod"].ToString();
                           dr["ime"] = reader["ime"].ToString();
                           dr["cijena_sa_porezom"] = reader["cijena_sa_porezom"].ToString();

                           GLOBALVARS.dt.Rows.Add(dr);

                           dataGridView1.DataSource = GLOBALVARS.dt;
                       }

                   }

                   if (GLOBALVARS.dt.Rows.Count == 0)
                   {
                       MessageBox.Show("Bar kod not exist!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       textBox1.Text = "";

                   }
               }


           }


这篇关于从ms SQL中选择并插入datagridview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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