在C#中以逗号分隔的文本框中将数据添加到sql中

时间:2018-03-21 08:51:38

标签: c# sql-server ado.net

我有一个文本框,其中包含以逗号分隔的数据。请参阅图片:

enter image description here

和一个添加数据的按钮。

数据以逗号分隔:E013-007,E013-021,E013-022,E013-048,E013-049,V039-034

我需要帮助才能使用C#将该文本框中的数据插入到SQL中。

Sql中的数据:

  

E013-007

     

E013-021

     

E013-022

     

...

我有代码但不起作用:

SqlConnection con = new SqlConnection(DbConnect.ConnectStr);
        SqlCommand cmd = new SqlCommand("multiple", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        con.Open();

        if (txtKMH.Text.Contains(","))//checking for you are entered single value or multiple values
        {
            string[] arryval = txtKMH.Text.Split(',');//split values with ‘,’
            int j = arryval.Length;
            int i = 0;
            for (i = 0; i < j; i++)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@Main_KMH", arryval[j]);
                cmd.Parameters.AddWithValue("@Main_Date", txtNgay.Text);
                cmd.ExecuteNonQuery();
            }
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Thành công!')</script>");
        }
        con.Close();

SQL:

USE [Database]
    GO
   SET ANSI_NULLS ON
   GO
   SET QUOTED_IDENTIFIER ON
   GO
   ALTER proc [dbo].[multiple] @Main_KMH nvarchar(50),@Main_Date DateTime
   AS
   Begin
   insert into MainVotes(Main_KMH,Main_Date) values(@Main_KMH,@Main_Date);
   End

2 个答案:

答案 0 :(得分:2)

将逗号分隔的字符串作为nvarchar(max)参数传递给sql server。然后处理这个以将其转换为行并使用它插入。您可以为此创建一个函数。

类似的东西:

CREATE FUNCTION dbo.BreakRows (@CommadelimitedString   varchar(1000))
RETURNS   @Result TABLE (Column1   VARCHAR(100))
AS
BEGIN
        DECLARE @IntLocation INT
        WHILE (CHARINDEX(',',    @CommadelimitedString, 0) > 0)
        BEGIN
              SET @IntLocation =   CHARINDEX(',',    @CommadelimitedString, 0)      
              INSERT INTO   @Result (Column1)
              --LTRIM and RTRIM to ensure blank spaces are   removed
              SELECT RTRIM(LTRIM(SUBSTRING(@CommadelimitedString,   0, @IntLocation)))   
              SET @CommadelimitedString = STUFF(@CommadelimitedString,   1, @IntLocation,   '') 
        END
        INSERT INTO   @Result (Column1)
        SELECT RTRIM(LTRIM(@CommadelimitedString))--LTRIM and RTRIM to ensure blank spaces are removed
        RETURN 
END
GO

来源here 现在在存储过程中使用它:

SELECT * FROM dbo.BreakStringIntoRows('Apple,Banana,Orange')

您必须稍微更改此代码。但这个想法可以用来实现你想要做的事情

答案 1 :(得分:0)

我们可以创建一个函数,使用XMl和SPlit()

逐行分割给定的逗号分隔字符串
CREATE FUNCTION [dbo].[udf_GetUnsplitData]
(
@string nvarchar(max)
)
RETURNS  @OutTable TABLE
(
DATA varchar(20)
)
AS
BEGIN

DECLARE @Temp AS TABLE
(
DATA nvarchar(max)
)
INSERT INTO @Temp
SELECT @string

INSERT INTO @OutTable
SELECT 
 Split.a.value('.','nvarchar(1000)') As DATA
FROM
(
SELECT 
CAST('<S>'+REPLACE(DATA,',','</S><S>')+'</S>' AS XML ) AS DATA
FROM @Temp
)A
CROSS APPLY DATA.nodes('S') AS Split(a)

RETURN
END
GO

SELECT [dbo].[udf_GetUnsplitData]('E013-007,E013-021,E013-022,E013-048,E013-049,V039-034')

RESULT

DATA
--------
E013-007
E013-021
E013-022
E013-048
E013-049
V039-034