如何一次插入一行

如何一次插入一行

本文介绍了如何一次插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过sqldatareader输入了test.cid,cdt,cc1,cc2,cc3,它运行正常。



我现在想要插入一次一行数据。



我在test.cid [i],test.cdt [i],test.cc1 [i],测试中出错.cc2 [i],test.cc3 [i]



我该怎么做?



MDFtoMDF.cs



I have inputted test.cid,cdt,cc1,cc2,cc3 thru a sqldatareader and it works fine.

I now want to "INSERT INTO" the data one row at a time.

I am getting errors with test.cid[i],test.cdt[i],test.cc1[i],test.cc2[i],test.cc3[i]

How do I do that?

MDFtoMDF.cs

public class MDFtoMDF : IDisposable
{
    public int cid { get; set; }
    public string cdt { get; set; }
    public string cc1 { get; set; }
    public string cc2 { get; set; }
    public string cc3 { get; set; }
    public void Dispose()
    {
        Dispose(true);
    }

    private void Dispose(bool disposable)
    {
        if (disposable)
        {
            // Free resources
        }
        // Prevents the destructor to be called if dispose is called.
        GC.SuppressFinalize(this);
    }

}



MDFtoMDF.aspx.cs


MDFtoMDF.aspx.cs

    protected void Button1_Click(object sender, EventArgs e)
{
    //ArrayList list = new ArrayList();
    using (MDFtoMDF test = new MDFtoMDF())
    {
        Int16 n = 0;
        String connStr = ConfigurationManager.ConnectionStrings["From"].ConnectionString;
        String cmdStr = "SELECT * FROM [Table2];";
        try
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
                {
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        dr.Read();
                        n += 1;
                        test.cid = n + 3;
                        test.cdt = (dr[1].ToString());
                        test.cc1 = (dr[2].ToString());
                        test.cc2 = (dr[3].ToString());
                        test.cc3 = (dr[4].ToString());
                    }
                    conn.Close();
                    cmd.Dispose();
                    conn.Dispose();
                }
            }
        }
        catch (Exception ex)
        {
            TextBox1.Text = ex.Message;
        }
        using (MDFtoMDF test = new MDFtoMDF())
    {
        for (int i = 0; i <= n-1; i++)
        {
            int16 dataid = test.cid[i];
            string datadatetime = test.cdt[i];
            string datacol1 = test.cc1[i];
            string datacol2 = test.cc2[i];
            string datacol3 = test.cc3[i];
            //SQL INSERT INTO
         }
    }

推荐答案

Quote:



while(dr.Read())

{

n + = 1;

test.cid = n + 3;

test.cdt =(dr [1] .ToString());

test.cc1 =(dr [2] .ToString());

test.cc2 =(dr [3] .ToString());

test.cc3 = (博士[4] .ToString());

}


while (dr.Read())
{
n += 1;
test.cid = n + 3;
test.cdt = (dr[1].ToString());
test.cc1 = (dr[2].ToString());
test.cc2 = (dr[3].ToString());
test.cc3 = (dr[4].ToString());
}


这篇关于如何一次插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 12:02