如何使用C#将数据保存到Db中 [英] How Can I Save Data Into Db By Using C#

查看:158
本文介绍了如何使用C#将数据保存到Db中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用c#编码将数据保存到数据表中。

我已通过以下编码将数据显示到网格视图中。

我想修改给定的代码将这些数据保存到数据表中,与网格视图上的显示相同。

谢谢。



 受保护  void  Button1_Click( object  sender, EventArgs e)
{
dt1.Columns.Add( SiteName typeof string ));
dt1.Columns.Add( State typeof string ));
dt1.Columns.Add( Cluster typeof string ));

for int i = 0 ; i < 10 ; i ++)
{
dt1.Rows.Add( A B C);
gvData.DataSource = dt1;
gvData.DataBind();
}
}

解决方案

查看示例,它具有gridview的完整操作。添加,更新,删除记录



http://www.dotnetgallery.com/kb/resource10-How-to-perform-insert-update-delete-and-select- rows-in-ASPNET-gridview-c.aspx [ ^ ]


 DataTable dt1 =  new  DataTable(); 

dt1.Columns.Add( SiteName typeof string ));
dt1.Columns.Add( State typeof string ));
dt1.Columns.Add( Cluster typeof string ));
for int i = 0 ; i < 10 ; i ++)
{
dt1.Rows .Add( A B C);
}





 gvData.DataSource = dt1; 
gvData.DataBind();





更新





 使用 System.Data.SqlClient; 


for int n = 0 ; n < dt1.Rows.Count; n ++)
{
try
{
SqlConnection con = new SqlConnection( 您的数据库连接字符串);
string col1 = Convert.ToString(dt1.Rows [n] [ 网站名称]);
string col2 = Convert.ToString(dt1.Rows [n] [ ]);
string col3 = Convert.ToString(dt1.Rows [n] [ 群集]);
string strQry = 插入表格( col1,col2,col1)值(' + col1 + ',' + col2 + ',' + col3 + ');

con.Open();
SqlCommand cmd = new SqlCommand(strQry,con);
cmd.ExecuteNonQuery();
}
catch (例外e1)
{
}
终于
{
con.Close();
}

}


通过这个 [ ^ ]

I have to save data into data table by using c# coding.
I have displayed data into grid view by given below coding.
I want to modify the given code to save those data into data table and same as display on grid view.
Thank You.

protected void Button1_Click(object sender, EventArgs e)
    {
dt1.Columns.Add("SiteName", typeof(string));
        dt1.Columns.Add("State", typeof(string));
        dt1.Columns.Add("Cluster", typeof(string));

        for (int i = 0; i < 10; i++)
        {
            dt1.Rows.Add("A", "B", "C");
            gvData.DataSource = dt1;
            gvData.DataBind();
        }
     }

解决方案

See the example, Its having full operation with gridview . Adding, Updating , Deleting records

http://www.dotnetgallery.com/kb/resource10-How-to-perform-insert-update-delete-and-select-rows-in-ASPNET-gridview-c.aspx[^]


  DataTable dt1 = new DataTable();

dt1.Columns.Add("SiteName", typeof(string));
dt1.Columns.Add("State", typeof(string));
dt1.Columns.Add("Cluster", typeof(string));
for (int i = 0; i < 10; i++)
{
dt1.Rows.Add("A", "B", "C");
}



gvData.DataSource = dt1;
gvData.DataBind();



Updated


using System.Data.SqlClient;


  for (int n = 0; n < dt1.Rows.Count; n++)
{
try
{
SqlConnection con = new SqlConnection("Your Database Connection String");
string col1 = Convert.ToString(dt1.Rows[n]["SiteName"]);
string col2 = Convert.ToString(dt1.Rows[n]["State"]);
string col3 = Convert.ToString(dt1.Rows[n]["Cluster"]);
string strQry = "insert into table(col1,col2,col1) values('" + col1 + "','" + col2 + "','" + col3 + "')";
                
con.Open();
SqlCommand cmd = new SqlCommand(strQry, con);
cmd.ExecuteNonQuery();
}
catch(exception e1)
{
}
finally
{
con.Close();
}

 }


go through this[^]


这篇关于如何使用C#将数据保存到Db中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆