将数据表设置为等效

时间:2015-06-02 15:28:49

标签: c# sql asp.net

这可能是一个简单的问题,但我对C#没有经验。

我有2个数据表,1个基本上是另一个的副本(比如查看信息的表)。要设置值,这就是我现在正在做的事情:

    string attribute1 = "";
    string attribute2 = "";
    string attribute3 = ""; 
    .....
    DataTable result = new DataTable();
    using (SqlConnection con = new SqlConnection("user id=user_id;password=pwd;server=serverstring;Trusted_Connection=yes;database=database;connection timeout=30"))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM table1 WHERE parameter=@identifying_parameter", con))
        {
            cmd.Parameters.AddWithValue("@identifying_parameter", "example");
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                attribute1 = Convert.ToString(reader["attribute1"]);
                attribute2 = Convert.ToString(reader["attribute2"]);
                attribute3 = Convert.ToString(reader["attribute3"]);
                .....
            }
            con.Close();
        }
    }
    using (SqlConnection con = new SqlConnection("user id=user_2;password=pwd;server=serverstring;Trusted_Connection=yes;database=database;connection timeout=30"))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO table2 (attribute1, attribute2, attribute3, ...)  VALUES(@attribute1, @attribute2, @attribute3, ...)", con))
        {
            cmd.Parameters.AddWithValue("@attribute1", attribute1);
            cmd.Parameters.AddWithValue("@attribute2", attribute2);
            cmd.Parameters.AddWithValue("@attribute3", attribute3);
            ....
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(result);
            con.Close();
            da.Dispose();
        }
     }

显然我可能有很多属性,那么是否有更简单的方法将表中的每个属性设置为在C#中相等?

2 个答案:

答案 0 :(得分:0)

您可以使用INSERT..INTO..SELECT

DataTable result = new DataTable();
using (SqlConnection con = new SqlConnection("user id=user_2;password=pwd;server=serverstring;Trusted_Connection=yes;database=database;connection timeout=30"))
{
    using (SqlCommand cmd = new SqlCommand(@"INSERT INTO table2 (attribute1, attribute2, attribute3, ...)  
        SELECT attribute1, attribute2, attribute3 ... FROM table1
        WHERE parameter=@identifying_parameter                  
        ", con))
    {
        cmd.Parameters.AddWithValue("@identifying_parameter", "example");
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(result);
        con.Close();
        da.Dispose();
    }
}

您可以使用*代替指定列名,但这不是一个好习惯。

答案 1 :(得分:0)

为了使第二个表与第一个表相同(或根据您的定义“等效”)(为了确定,我们称之为sourceTable),您可以使用SqlBulkCopy.WriteToServer方法(DataTable)(re:{ {3}})

using (SqlConnection YourConnection= new SqlConnection(YourConnectionString))
{
       YourConnection.Open();
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(YourConnection))
        {
            bulkCopy.DestinationTableName = "dbo.YourDestinationTable";

            try
            {
                // Write from the source to the destination.
                bulkCopy.WriteToServer(sourceTable);
            }
            catch (Exception ex) { }
        }
}

要获取sourceTable,您可以使用以下代码段:

DataTable sourceTable = SqlReadDB(YourConnString, "SELECT *")

private static DataTable SqlReadDB(string ConnString, string SQL)
{
    DataTable _dt;
    try
    {
        using (SqlConnection _connSql = new SqlConnection(ConnString))
        {
            using (SqlCommand _commandl = new SqlCommand(SQL, _connSql))
            {
                _commandSql.CommandType = CommandType.Text;
                _connSql.Open();
                using (SqlCeDataReader _dataReaderSql = _commandSql.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    _dt = new DataTable();
                    _dt.Load(_dataReaderSqlCe);
                    _dataReaderSql.Close();
                }
            }
            _connSqlCe.Close();
            return _dt;
        }
    }
    catch { return null; }
}

}

希望这可能会有所帮助。