如何拆分逗号分隔值

时间:2023-01-11 19:20:39

I have text-box inside a gridview and i am performing and insert statement looping through every row in my gridview. My issue now is that i can have multiple names in the text-box like this:

我在gridview中有文本框,我正在执行并插入语句循环遍历gridview中的每一行。我现在的问题是,我可以在文本框中有多个名称,如下所示:

John Carter, Mike David, John Edward,

so how can i split and insert each individual name into my table with the same ID? For instance, if the current row has ID =12 then my table will look like this:

那么如何将每个单独的名称拆分并插入到具有相同ID的表中?例如,如果当前行的ID = 12,那么我的表将如下所示:

ID      Full_Name
12     John Carter
12     Mike David
12     John Edward

here is my code:

这是我的代码:

 foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                Label ID = row.FindControl("lbl_ID") as Label;                
                TextBox myUID = row.FindControl("txt_UID") as TextBox;
                string Full_Name = Request.Form[row.FindControl("txt_UID").UniqueID];

                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"if not exists(select ID from myTable where ID = @ID)
                                   insert into myTable(ID, Full_Name) values(@ID, @Full_Name)
                                    else update myTable set Full_Name =@Full_Name where ID =@ID";

                cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
                cmd.Parameters.Add("@Full_Name", SqlDbType.VarChar).Value = Full_Name.ToString();

                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

            }
}

3 个解决方案

#1


0  

Here's a very rough/quick notion of what I'm suggesting in the comment above. have not had a chance to test it.

这是我在上面评论中建议的非常粗略/快速的概念。没有机会测试它。

 foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                Label ID = row.FindControl("lbl_ID") as Label;                
                TextBox myUID = row.FindControl("txt_UID") as TextBox;
                string Full_Name = Request.Form[row.FindControl("txt_UID").UniqueID];

                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"if not exists(select ID from myTable where ID = @ID)
                                   insert into myTable(ID, Full_Name) values(@ID, @Full_Name)
                                    else update myTable set Full_Name =@Full_Name where ID =@ID";
                cmd.Connection = con;
                con.Open();
                foreach(string currentName in Full_Name.Split(','))
                {
                    if (currentName!="")
                    {
                        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
                        cmd.Parameters.Add("@Full_Name", SqlDbType.VarChar).Value = currentName;

                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                }
                con.Close();

            }
}

#2


2  

string str = "John Carter, Mike David, John Edward,";
string[] names = str.Split(',');
foreach (string name in names)
{
    if (name.Equals(""))
        continue;

    ///dbstuff
    ///insert into myTable(ID, Full_Name) values(@ID, @name)
    ///etc, etc
}

This strongly assumes that ID is not a primary key. As long as ID is not unique and not a key, this sort of methodology should work.

这强烈假设ID不是主键。只要ID不是唯一的而不是密钥,这种方法就可以工作。

#3


1  

You can execute the query with the use of loop like this

你可以像这样使用循环来执行查询

foreach (string name in Full_Name.Split(','))
{
  cmd.CommandText = @"if not exists(select ID from myTable where ID = @ID)
                        insert into myTable(ID, Full_Name) values(@ID, @Full_Name)
                      else update myTable set Full_Name =@Full_Name where ID =@ID";

  cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
  cmd.Parameters.Add("@Full_Name", SqlDbType.VarChar).Value = name

  cmd.Connection = con;
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}

#1


0  

Here's a very rough/quick notion of what I'm suggesting in the comment above. have not had a chance to test it.

这是我在上面评论中建议的非常粗略/快速的概念。没有机会测试它。

 foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                Label ID = row.FindControl("lbl_ID") as Label;                
                TextBox myUID = row.FindControl("txt_UID") as TextBox;
                string Full_Name = Request.Form[row.FindControl("txt_UID").UniqueID];

                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"if not exists(select ID from myTable where ID = @ID)
                                   insert into myTable(ID, Full_Name) values(@ID, @Full_Name)
                                    else update myTable set Full_Name =@Full_Name where ID =@ID";
                cmd.Connection = con;
                con.Open();
                foreach(string currentName in Full_Name.Split(','))
                {
                    if (currentName!="")
                    {
                        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
                        cmd.Parameters.Add("@Full_Name", SqlDbType.VarChar).Value = currentName;

                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                }
                con.Close();

            }
}

#2


2  

string str = "John Carter, Mike David, John Edward,";
string[] names = str.Split(',');
foreach (string name in names)
{
    if (name.Equals(""))
        continue;

    ///dbstuff
    ///insert into myTable(ID, Full_Name) values(@ID, @name)
    ///etc, etc
}

This strongly assumes that ID is not a primary key. As long as ID is not unique and not a key, this sort of methodology should work.

这强烈假设ID不是主键。只要ID不是唯一的而不是密钥,这种方法就可以工作。

#3


1  

You can execute the query with the use of loop like this

你可以像这样使用循环来执行查询

foreach (string name in Full_Name.Split(','))
{
  cmd.CommandText = @"if not exists(select ID from myTable where ID = @ID)
                        insert into myTable(ID, Full_Name) values(@ID, @Full_Name)
                      else update myTable set Full_Name =@Full_Name where ID =@ID";

  cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
  cmd.Parameters.Add("@Full_Name", SqlDbType.VarChar).Value = name

  cmd.Connection = con;
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}