小编Ali*_*007的帖子

Cql批量复制/插入C#

我是JSON和SQLBulkCopy的新手.我有一个JSON格式的POST数据,我希望使用C#在Microsoft SQL中批量复制/插入.

JSON格式:

{
    "URLs": [{
        "url_name": "Google",
        "url_address": "http://www.google.com/"
    },
    {
        "url_name": "Yahoo",
        "url_address": "http://www.yahoo.com/"
    },
    {
        "url_name": "FB",
        "url_address": "http://www.fb.com/"
    },
    {
        "url_name": "MegaSearches",
        "url_address": "http://www.megasearches.com/"
    }]
}
Run Code Online (Sandbox Code Playgroud)

类别:

public class UrlData
{
    public List<Url> URLs {get;set;}
}

public class Url
{
    public string url_address {get;set;}
    public string url_name {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能有效地做到这一点?

.net c# sql sqlbulkcopy

17
推荐指数
2
解决办法
6万
查看次数

哪一个更好 - 在ASPX页面或CodeBehind中声明SqlDataSource?

哪一个更好 - 在ASPX页面或CodeBehind中声明SqlDataSource?

方法#A.作为程序员,您可以在.aspx页面中定义SqlDataSource,例如:

<asp:SqlDataSource ID="Sql_ID" runat="server" ConnectionString="<%$ ConnectionStrings:Con_Str %>"
    SelectCommand="SELECT * FROM [table_name]">
    <SelectParameters>
        <asp:Parameter Name="user_id"/>
    </SelectParameters>
</asp:SqlDataSource>
Run Code Online (Sandbox Code Playgroud)

方法#B.此外,您可以在C#CodeBehind中执行此操作

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Con_Str"].ToString()))
{
    string qry = SELECT * FROM [table_name];

    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    using (SqlCommand cmd = new SqlCommand(qry, conn))
    {
        cmd.Parameters.Add("@user_id", SqlDbType.UniqueIdentifier).Value = user_id;
        da.SelectCommand = cmd;

        try
        {
            conn.Open();
            da.Fill(dt);
            conn.Close();
        }
        catch
        {
            // Do something ;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

哪种方法[A或B]更好?为什么?

.net c# sql asp.net datasource

2
推荐指数
1
解决办法
2006
查看次数

标签 统计

.net ×2

c# ×2

sql ×2

asp.net ×1

datasource ×1

sqlbulkcopy ×1