如何获取值12.0保存并显示在数据网格视图中 [英] How do I get values 12.0 save and displayed in data grid view

查看:85
本文介绍了如何获取值12.0保存并显示在数据网格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private void button1_Click(object sender,EventArgs e)
{
if(comboBoxstaff.Text == string.Empty)
{
MessageBox。显示(请选择测量员); //不要让thecombobox为空
返回;
}
else if(comboBoxcompondrubber.Text == string.Empty)
{
MessageBox.Show(Please Select Compond Rubber); //不要让thecombobox空
返回;

}
else if(textBox1.Text == string.Empty)
{
MessageBox.Show(Please Key in WS thickness); // not not让thetextbox为空
返回;

}
else if(textBox2.Text == string.Empty)
{
MessageBox.Show(请输入GS厚度); // not not让thecombobox为空

}
SQLiteConnection insertsess = new SQLiteConnection(Data Source = | DataDirectory | \\test1db);
string insert12 =INSERT INTO thickness(GaugeMan,Dateandtime,CompondRubber,GSthickness,WSthicknes)VALUES('+ comboBoxstaff.Text +','+ label2.Text +','+ comboBoxcompondrubber.Text +','+ textBox2.Text +','+ textBox1.Text +'); // insert statment
SQLiteCommand ins1 = new SQLiteCommand(insert12,insertsess);
insertsess.Open();
ins1.ExecuteNonQuery();
MessageBox.Show(数据已保存); //显示消息何时被保存

SQLiteConnection sesscheck = new SQLiteConnection(Data Source = | DataDirectory | \\ test1db);
SQLiteCommand chk1;
chk1 = sesscheck.CreateCommand();
chk1.CommandText =SELECT GaugeMan,Dateandtime,CompondRubber,GSthickness,WSthicknes FROM thickness WHERE CompondRubber ='+ comboBoxcompondrubber.Text.Trim()+';
sesscheck.Open();
DataTable thicknessTable = new DataTable();
// DataTable thicknessTable = new DataTable();
SQLiteDataReader reader = chk1.ExecuteReader();
thicknessTable.Load(读者);
// SQLiteDataReader reader1 = chk2.ExecuteReader();
//thicknessTable.Load(reader1);
sesscheck.Close();

dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;

}





我的尝试:



我曾尝试使用数据适配器,但它没有用我尝试使用数据读卡器,但我不确定。



我想要保存值12.0并显示到数据网格视图中此时如果我键12.1它也只能像12.0那样输入并在数据网格视图中显示为12

解决方案

Quote:

我想将值12.0保存并显示到数据网格视图中



您知道我们无法访问您的数据库吗?



  string  insert12 =  插入厚度(GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes)VALUES(' + comboBoxstaff.Text +  ',' + label2.Text +  ',' + comboBoxcompondrubber.Text +  ',' + textBox2.Text +  ',' + textBox1.Text +  ');  //   insert statment  



不是解决方案你的问题,但你有另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


查看我的帖子解决方案面临Sql插入错误 [ ^ ]

private void button1_Click(object sender, EventArgs e)
    {
        if (comboBoxstaff.Text == string.Empty)
        {
            MessageBox.Show("Please Select gaugeman");  // not to let thecombobox empty
            return;
        }
        else if (comboBoxcompondrubber.Text == string.Empty)
        {
            MessageBox.Show("Please Select Compond Rubber");// not to let thecombobox empty
            return;

        }
        else if (textBox1.Text == string.Empty)
        {
            MessageBox.Show("Please Key in W.S thickness");// not to let thetextbox empty
            return;

        }
        else if (textBox2.Text == string.Empty)
        {
            MessageBox.Show("Please Key in G.S thickness");// not to let thecombobox empty

        }
        SQLiteConnection insertsess = new SQLiteConnection("Data Source=|DataDirectory|\\test1db");
        string insert12 = "INSERT INTO thickness (GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes) VALUES ('" + comboBoxstaff.Text + "','" + label2.Text + "', '" + comboBoxcompondrubber.Text + "', '" + textBox2.Text + "', '" + textBox1.Text + "')";   //insert statment
        SQLiteCommand ins1 = new SQLiteCommand(insert12, insertsess);
        insertsess.Open();
        ins1.ExecuteNonQuery();
        MessageBox.Show("Data had been saved");// showed when the message is being saved

        SQLiteConnection sesscheck = new SQLiteConnection("Data Source=|DataDirectory|\\test1db");
        SQLiteCommand chk1;
        chk1 = sesscheck.CreateCommand();
        chk1.CommandText = "SELECT GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes FROM thickness WHERE CompondRubber = '" + comboBoxcompondrubber.Text.Trim() + "'";
       sesscheck.Open();
        DataTable thicknessTable = new DataTable();
        //DataTable thicknessTable = new DataTable();
        SQLiteDataReader reader = chk1.ExecuteReader();
        thicknessTable.Load(reader);
        //SQLiteDataReader reader1 = chk2.ExecuteReader();
        //thicknessTable.Load(reader1);
        sesscheck.Close();

        dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;

    }



What I have tried:

I had tried using data adapter but it didn't work I tried using data reader but I am not sure.

I want to get the value 12.0 to be saved and displayed into data grid view at the moment if I key 12.1 it also works only like 12.0 it will be entered and displayed as 12 at the data grid view

解决方案

Quote:

I want to get the value 12.0 to be saved and displayed into data grid view


Do you know that we don't have access to your database ?

string insert12 = "INSERT INTO thickness (GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes) VALUES ('" + comboBoxstaff.Text + "','" + label2.Text + "', '" + comboBoxcompondrubber.Text + "', '" + textBox2.Text + "', '" + textBox1.Text + "')";   //insert statment


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


See my solution to your post Sql interjection error faced[^]


这篇关于如何获取值12.0保存并显示在数据网格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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