多次插入SQL [英] Multiple Insert to SQL

查看:99
本文介绍了多次插入SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 尝试 
{
如果 (txtDesc1.Text ==
{
}
else
{
con1 = new SqlDbConnect();
strSQL = 插入样本(id,描述,剂量,数量,销售,金额);
strSQL + = select' + labelTransID.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDesc1.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDosage1.Text.Replace( ' '')+ ';
strSQL + = ,' + txtQty1.Text.Replace( ' '')+ ';
strSQL + = ,' + txtSelling1.Text.Replace( ' '')+ ';
strSQL + = ,' + txtAmount1.Text.Replace( ' '')+ ';
con1.SqlQuery(strSQL);
con1.NonQueryEx();
con1.Close();
}

if (txtDesc2.Text == < span class =code-string>

{
}
else
{
con2 = new SqlDbConnect();
strSQL = 插入样本(id,描述,剂量,数量,销售,金额);
strSQL + = select' + labelTransID.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDesc2.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDosage2.Text.Replace( ' '')+ ';
strSQL + = ,' + txtQty2.Text.Replace( ' '')+ ';
strSQL + = ,' + txtSelling2.Text.Replace( ' '')+ ';
strSQL + = ,' + txtAmount2.Text.Replace( ' '')+ ';
con2.SqlQuery(strSQL);
con2.NonQueryEx();
con2.Close();
}
if (txtDesc3.Text ==
{
}
else
{
con3 = new SqlDbConnect();
strSQL = 插入样本(id,描述,剂量,数量,销售,金额);
strSQL + = select' + labelTransID.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDesc3.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDosage3.Text.Replace( ' '')+ ';
strSQL + = ,' + txtQty3.Text.Replace( ' '')+ ';
strSQL + = ,' + txtSelling3.Text.Replace( ' '')+ ';
strSQL + = ,' + txtAmount3.Text.Replace( ' '')+ ';
con3.SqlQuery(strSQL);
con3.NonQueryEx();
con3.Close();
}
if (txtDesc4.Text ==
{
}
else
{
con4 = new SqlDbConnect();
strSQL = 插入样本(id,描述,剂量,数量,销售,金额);
strSQL + = select' + labelTransID.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDesc4.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDosage4.Text.Replace( ' '')+ ';
strSQL + = ,' + txtQty4.Text.Replace( ' '')+ ';
strSQL + = ,' + txtSelling4.Text.Replace( ' '')+ ';
strSQL + = ,' + txtAmount4.Text.Replace( ' '')+ ';
con4.SqlQuery(strSQL);
con4.NonQueryEx();
con4.Close();
}
if (txtDesc5.Text ==
{
}
else
{
con5 = new SqlDbConnect();
strSQL = 插入样本(id,描述,剂量,数量,销售,金额);
strSQL + = select' + labelTransID.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDesc5.Text.Replace( ' '')+ ';
strSQL + = ,' + txtDosage5.Text.Replace( ' '')+ ';
strSQL + = ,' + txtQty5.Text.Replace( ' '')+ ';
strSQL + = ,' + txtSelling5.Text.Replace( ' '')+ ';
strSQL + = ,' + txtAmount5.Text.Replace( ' '')+ ';
con5.SqlQuery(strSQL);
con5.NonQueryEx();
con5.Close();
}
}
catch
{
MessageBox.Show( 已成功保存 完成!,MessageBoxButtons.OK);
}





这里是我保存的代码...问题是当其他文本框为空时。不会保存到SQL数据库。有没有其他方法来最小化这些代码.ty

解决方案

尝试运行此示例并发现代码中的错误:

 使用系统; 

public class 计划
{
public static void Main()
{
string str = 我们是一个国家;
string str1 = str.Replace( ' '');
Console.WriteLine(str);
Console.WriteLine(str1);
}
}



您还应该

使用参数化查询来防止SQL Server中的SQL注入攻击
[ ^ ]


我建​​议你首先制作要在前端插入的所有数据的xml,然后将其发送到程序中,之后你可以从那个xml中插入它。

try
            {
                if (txtDesc1.Text == "")
                {
                }
                else
                {
                    con1 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount1.Text.Replace("'", "''") + "'";
                    con1.SqlQuery(strSQL);
                    con1.NonQueryEx();
                    con1.Close();
                }

                if (txtDesc2.Text == "")
                {
                }
                else
                {
                    con2 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount2.Text.Replace("'", "''") + "'";
                    con2.SqlQuery(strSQL);
                    con2.NonQueryEx();
                    con2.Close();
                }
                if (txtDesc3.Text == "")
                {
                }
                else
                {
                    con3 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount3.Text.Replace("'", "''") + "'";
                    con3.SqlQuery(strSQL);
                    con3.NonQueryEx();
                    con3.Close();
                }
                if (txtDesc4.Text == "")
                {
                }
                else
                {
                    con4 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount4.Text.Replace("'", "''") + "'";
                    con4.SqlQuery(strSQL);
                    con4.NonQueryEx();
                    con4.Close();
                }
                if (txtDesc5.Text == "")
                {
                }
                else
                {
                    con5 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount5.Text.Replace("'", "''") + "'";
                    con5.SqlQuery(strSQL);
                    con5.NonQueryEx();
                    con5.Close();
                }
            }
            catch
            {
                MessageBox.Show("Successfully Saved", "Completed !", MessageBoxButtons.OK);
            }



here is my code for saving...problem is when the other textboxes are empty. won't save to sql database. is there any other approach to minimize this lot of codes.ty

解决方案

Try running this example and discover the mistakes in your code:

using System;

public class Program
{
    public static void Main()
    {
        string str = "we're a nation";
        string str1 = str.Replace("'","''");
        Console.WriteLine(str);
        Console.WriteLine(str1);
    }
}


You also should
Use Parameterized queries to prevent SQL Injection Attacks in SQL Server
[^]


I suggest you, that first made xml of all data which you want to insert on front end, then send it into procedure and after that you can insert it from that xml.


这篇关于多次插入SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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