第二行数据不会进入C#中的数据库 [英] The second row data do not enter into database in C#

查看:99
本文介绍了第二行数据不会进入C#中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第二行数据不会在c#中输入数据库更正它PLZ



我尝试过:



SqlConnection con = new SqlConnection(@Data Source = DESKTOP-C8H8UQI \ SQLEXPRESS; Initial Catalog = IMSDatabase; Integrated Security = True);



public Form2()

{

InitializeComponent();

}



private void button4_Click(object sender,EventArgs e)

{



dataGridView2.Rows.Add(textBox3。文字);



}



private void button1_Click(object sender,EventArgs e)

{

for(int j = 0; j< dataGridView2.Rows.Count; j ++)

{

string query =插入粗糙(itemName)值('+ dataGridView2.Rows [j] .Cells [0] .Value +');

SqlCommand cmd = new SqlCommand(query,con);

con.Open();

cmd.ExecuteNonQuery();

con.Close ();

dataGridView2.Rows.Clear();

the second row data do not enter into database in c# correct it plz

What I have tried:

SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-C8H8UQI\SQLEXPRESS;Initial Catalog=IMSDatabase;Integrated Security=True");

public Form2()
{
InitializeComponent();
}

private void button4_Click(object sender, EventArgs e)
{

dataGridView2.Rows.Add(textBox3.Text);

}

private void button1_Click(object sender, EventArgs e)
{
for (int j = 0; j < dataGridView2.Rows.Count; j++)
{
string query = "insert into Rough(itemName)values('" + dataGridView2.Rows[j].Cells[0].Value + "')";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
dataGridView2.Rows.Clear();

推荐答案

嗯。

看看你的代码:

Um.
Look at your code:
for (int j = 0; j < dataGridView2.Rows.Count; j++)
    {
    string query = "insert into Rough(itemName)values('" + dataGridView2.Rows[j].Cells[0].Value + "')";
...
    dataGridView2.Rows.Clear();

所以一旦你绕过循环一次,你已经从中删除了所有其他行...移动在循环之后清楚,并且它;它会更好地工作。



但是......这里有其他一些错误。

1)不要硬编码连接字符串 - 它们需要在配置文件或类似文件中,或者你必须为每个新安装重新编译你的应用程序。

2)Connection,Command,和类似的对象是稀缺资源 - 你应该在完成后处理它们。最简单的方法是使用块块:

So once you've been round the loop once, you have removed all the other rows from it ... Move the clear to after the loop, and it;ll work better.

But ... there are a load of other things wrong here.
1) Don't hard-code connection strings - they need to be in a config file or similar, or you have to recompile your app for each new installation.
2) Connection, Command, and similar objects are scarce resources - you should Dispose of them when you are finished. The simplest way is to us a using block:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }



3)永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:


3) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于第二行数据不会进入C#中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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