SQL中选择并插入datagridview

SQL中选择并插入datagridview

本文介绍了从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 = "";
}
}

}

推荐答案


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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 01:18