如何将3个值分配到一个字符串中

本文关键字:一个 字符串 3个 分配 | 更新日期: 2023-09-27 18:24:45

我在赋值时遇到问题。

 string userlanguages = string.Empty;
        try
        {
            SqlCommand cmd = new SqlCommand("SELECT [s_langName] from [dbo].[tx_UserLanguages] WHERE [s_ID] ='" + Class1.EmployeeId + "'", objcon);
            if (objcon.State == ConnectionState.Closed) objcon.Open();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet myDS = new DataSet();
            adp.Fill(myDS);
            userlanguages = myDS.Tables[0].Rows[0]["[s_langName]"].ToString();
        }
        catch (SqlException exc)
        {
            Response.Write(exc.Message);
        }
        finally
        {
            objcon.Close();
            Class1.langKnwn = userlanguages;
        }
    }

假设myDs包含三个值。那么我将如何将它们分配给一个字符串(在本例中为"userlanguages")。我在另一个页面中使用Class1.langKwn来显示用户选择的语言(在许多情况下可能不止一种语言)。

请帮帮我。

如何将3个值分配到一个字符串中

像这样的东西怎么样

List<string> languages = new List<string>();
for (int iRow = 0; iRow < myDS.Tables[0].Rows.Count; iRow++)
    languages.Add(myDS.Tables[0].Rows[iRow]["[s_langName]"].ToString());
userlanguages = String.Join(",", languages);
        SqlCommand cmd = new SqlCommand("SELECT [s_langName] from [dbo].[tx_UserLanguages] WHERE [s_ID] ='" + Class1.EmployeeId + "'", objcon);
        if (objcon.State == ConnectionState.Closed) objcon.Open();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet myDS = new DataSet();
        adp.Fill(myDS);
        for(int ndx=0;ndx<myDS.Tables[0].Rows.Count;ndx+=1)
         {
             if(ndx==0)
               {
                  userlanguages = myDS.Tables[0].Rows[ndx]["[s_langName]"].ToString();
               }
               else
               {
                  userlanguages = userlanguages+","+ myDS.Tables[0].Rows[ndx]["[s_langName]"].ToString();
               }
         }
userlanguages = string.Join(",",  myDS.Tables[0].AsEnumerable()
                                      .Select(row => row.Field<string>("[s_langName]"))
                                      .ToArray());

如果我正确理解您的问题,在您的情况下使用SqlDataReader可能是更好的选择。

using (SqlConnection objcon= new SqlConnection(connectionString))
{
    SqlCommand cmd = new SqlCommand("SELECT [s_langName] from [dbo].[tx_UserLanguages] WHERE [s_ID] = @id", objcon);
    cmd.Parameters.AddWithValue("@id", Class1.EmployeeId);
    connection.Open();
    SqlDataReader reader = cmd .ExecuteReader();
    string userlanguages = string.Empty;
    while (reader.Read())
    {
        string str = reader.GetString(0);
        userlanguages = String.Join(",", userlanguages);
    }
    reader.Close();
}

您应该始终使用参数化查询。这种字符串串联对SQL注入攻击是开放的。

试试这个

使用using System.Text;名称映射

然后把代码写进你的区块

 StringBuilder sb = new StringBuilder();
        for (int iRow = 0; iRow < myDS.Tables[0].Rows.Count; iRow++)
        {
            sb.Append(myDS.Tables[0].Rows[iRow]["[s_langName]"].ToString());
            sb.Append(",");
        }

//以获得末端的字符串string myString = sb.ToString();

myString= myString.Substring(0, myString.Length - 1);