从sql获取值并放入一些字符串

时间:2015-01-12 03:33:33

标签: c# sql sql-server

如何从sql获取值并在其上放置一些字符串。这是我的代码

public string GenerateInsertStatement2(DataRow[] columns, string tableName, string dbName)
    {
        var columnsString = "";

        var listColumns = columns.Select(a => a["COLUMN_NAME"].ToString()).ToList();

        foreach (DataRow column in columns)
        {
            columnsString += column["COLUMN_NAME"] + ",";
            //if (column["DATA_TYPE"].ToString().ToLower().Contains("numeric"))
        }
        columnsString = StripCommaOnLastCharacter(columnsString);

        //columnsString = ConcatinateListOfString(listColumns);


        string sqlQuery = string.Format("SELECT {0} from {1}", columnsString, tableName);



        var sqlConnection = new SqlConnection(GetConnectionString(dbName));
        var sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
        var sqlDa = new SqlDataAdapter(sqlCommand);
        DataTable dt = new DataTable();
        sqlDa.Fill(dt);
        string app = "{";
        string apps = "}";
        string apps1 = ";";
        var sqlString = new StringBuilder(); 
        var columnValues = string.Empty;

        foreach (DataRow row in dt.Rows)
        {
            sqlString.Append(string.Format("var record = new {1} \r\n {0} ", app, tableName));


            columnValues = "";

            foreach (DataColumn column in dt.Columns)
            {
                columnValues += string.Format(" {0} = {1} \r\n",columnsString, row[column.ColumnName].ToString());        
            }

              columnValues = StripCommaOnLastCharacter(columnValues);
            //  columnValues = ConcatinateListOfString(listColumns);

            sqlString.Append(string.Format("\r\n{1}  \r\n {0}{2} \r\n ", apps, columnValues,apps1 ));
            sqlString.Append(string.Format("Context.EntitySet<{0}>().AddOrUpdate(c=> new {1} {4} {2}, record){3} \r\n",tableName,app,apps,apps1,columnsString));
            sqlString.Append("\r\n");

        }

我想要输出从sql获取2个相似的值并使它们相等,例如第一行1和2获取它们的值并使其成为字符串(&#34;第1行=第2行和第34行; )必须是它的输出

0 个答案:

没有答案