سلام بر تمامی برنامه نویسان عزیز 😉 در این مطلب آموزش افزودن ردیف به DataTable با DataRow در سی شارپ #C را آماده کردم. انشاالله که این آموزش برای شما مفید باشد.
کد افزودن ردیف به DataTable با DataRow در سی شارپ #C :
using System;
using System.Data;
using System.Data.SqlClient;
class ModifyDataTable
{
static void Main(string[] args)
{
string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @"select * from employee where country = 'UK'";
SqlConnection conn = new SqlConnection(connString);
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employees");
DataTable dt = ds.Tables["employees"];
dt.Columns["firstname"].AllowDBNull = true;
dt.Rows[0]["city"] = "w";
DataRow newRow = dt.NewRow();
newRow["firstname"] = "R";
newRow["lastname"] = "B";
newRow["titleofcourtesy"] = "Sir";
newRow["city"] = "B";
newRow["country"] = "USA";
dt.Rows.Add(newRow);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0} {1} {2}",
row["firstname"].ToString().PadRight(15),
using System.Data;
using System.Data.SqlClient;
class ModifyDataTable
{
static void Main(string[] args)
{
string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
string sql = @"select * from employee where country = 'UK'";
SqlConnection conn = new SqlConnection(connString);
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employees");
DataTable dt = ds.Tables["employees"];
dt.Columns["firstname"].AllowDBNull = true;
dt.Rows[0]["city"] = "w";
DataRow newRow = dt.NewRow();
newRow["firstname"] = "R";
newRow["lastname"] = "B";
newRow["titleofcourtesy"] = "Sir";
newRow["city"] = "B";
newRow["country"] = "USA";
dt.Rows.Add(newRow);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0} {1} {2}",
row["firstname"].ToString().PadRight(15),