DB

using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace DB { class Program { static void Main(string[] args) { string strCn = "Data Source=tonqthonq\\tt97;Initial Catalog=DANH_BA;Integrated Security=True";// đây là chuổi kết nối string strSql = "Select * from Person"; ConnectBD test = new ConnectBD(strCn, strSql); if (test.con.State == System.Data.ConnectionState.Open) Console.Write("Connect Success"); Console.ReadKey(); /*object[] dataRow = new object[test.reader.FieldCount]; while (test.reader.Read()) { int cols = test.reader.GetValues(dataRow); for (int i = 0; i < cols; i++) Console.Write("| {0} ", dataRow[i]); Console.WriteLine(); } Console.ReadKey();*/ //------- SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = test.cmd; DataSet ds = new DataSet("PersonContacts"); DataTable personTable = new DataTable("Person"); DataColumn col = new DataColumn(); col.DataType = typeof(System.Int64); col.ColumnName = "ID"; col.ReadOnly = true; col.Unique = true; // values must be unique col.AutoIncrement = true; // keys are assigned automatically col.AutoIncrementSeed = -1; // first key starts with -1 col.AutoIncrementStep = -1; personTable.Columns.Add(col); personTable.PrimaryKey = new DataColumn[] { col }; col = new DataColumn(); col.DataType = typeof(string); col.ColumnName = "FirstName"; personTable.Columns.Add(col); col = new DataColumn(); col.DataType = typeof(string); col.ColumnName = "Name"; personTable.Columns.Add(col); ds.Tables.Add(personTable); DataTable contactTable = new DataTable("Contact"); col = new DataColumn(); col.DataType = typeof(System.Int64); col.ColumnName = "ID"; contactTable.Columns.Add(col); col = new DataColumn(); col.DataType = typeof(string); col.ColumnName = "FirstName"; contactTable.Columns.Add(col); col = new DataColumn(); col.DataType = typeof(string); col.ColumnName = "fullname"; contactTable.Columns.Add(col); col = new DataColumn(); col.DataType = typeof(string); col.ColumnName = "email"; contactTable.Columns.Add(col); // ds.Tables.Add(personTable); ds.Tables.Add(contactTable); DataColumn parentCol = ds.Tables["Person"].Columns["ID"]; DataColumn childCol = ds.Tables["Contact"].Columns["ID"]; DataRelation rel = new DataRelation("PersonHasContacts", parentCol, childCol); ds.Relations.Add(rel); adapter.Fill(ds, "Person"); foreach (DataRow row in personTable.Rows) { System.Console.WriteLine("\n {0} {1} {2}", row["ID"], row["Firsname"],row["fullname"]); Console.ReadKey(); } } } } public class ConnectBD { public SqlConnection con; public SqlCommand cmd; String strcn; public SqlDataReader reader; public ConnectBD(String strcn, String cm) { con = new SqlConnection(); con.ConnectionString = strcn; con.Open(); cmd = con.CreateCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = cm; //reader = cmd.ExecuteReader(); //------------------------------- } }
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace DB
{
class Program
{

static void Main(string[] args)
{
string strCn = "Data Source=tonqthonq\\tt97;Initial Catalog=DANH_BA;Integrated Security=True";// đây là chuổi kết nối
string strSql = "Select * from Person";
ConnectBD test = new ConnectBD(strCn, strSql);
if (test.con.State == System.Data.ConnectionState.Open) Console.Write("Connect Success");
Console.ReadKey();
/*object[] dataRow = new object[test.reader.FieldCount];
while (test.reader.Read())
{
int cols = test.reader.GetValues(dataRow);
for (int i = 0; i < cols; i++) Console.Write("| {0} ", dataRow[i]);
Console.WriteLine();
}
Console.ReadKey();*/

//-------
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = test.cmd;
DataSet ds = new DataSet("PersonContacts");
DataTable personTable = new DataTable("Person");
DataColumn col = new DataColumn();
col.DataType = typeof(System.Int64);
col.ColumnName = "ID";
col.ReadOnly = true;
col.Unique = true; // values must be unique
col.AutoIncrement = true; // keys are assigned automatically
col.AutoIncrementSeed = -1; // first key starts with -1
col.AutoIncrementStep = -1;
personTable.Columns.Add(col);
personTable.PrimaryKey = new DataColumn[] { col };
col = new DataColumn();
col.DataType = typeof(string);
col.ColumnName = "FirstName";
personTable.Columns.Add(col);
col = new DataColumn();
col.DataTyp

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.