日期:2014-05-17 浏览次数:20724 次
class Program
{
static void Main(string[] args)
{
SqlConnection cnn = new SqlConnection("Data Source=(local);Initial Catalog=liyang;uid=sa;pwd=123456;");
string sqlstr = "select * from [liyang].[dbo].[student] ";
try
{
cnn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, cnn);
DataSet ds = new DataSet();
sda.Fill(ds);//将执行结果填充到Dataset中,会隐藏打开SqlConnection并执行SQL等操作
Console.WriteLine("姓名\t\t班级");
int cnt = ds.Tables[0].Rows.Count;//读取行数
for (int ix = 0; ix != cnt; ++ix)
Console.WriteLine("{0}\t\t{1}", ds.Tables[0].Rows[ix].ItemArray[1],//第一个表里第几行第2列
ds.Tables[0].Rows[ix].ItemArray[2]);//循环读取数据,注意索引范围
DataTable mytable = ds.Tables[0];
foreach (DataRow myRow in mytable.Rows)
{
myRow["姓名"] =myRow["姓名"]+ "1";
}
SqlCommandBuilder mysqlCommanBuilder = new SqlCommandBuilder(sda);
sda.Update(ds);//更新数据库 操作