|
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication2 { class Program { static void Main() { string connectionString = GetConnectionString(); //定义数据库连接对象 SqlConnection connection = new SqlConnection(connectionString); //定义SQL字符串 String MySQL = "Select * From student"; //定义数据适配器对象 SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, connection); { try { //打开数据库连接 connection.Open(); MyAdapter.SelectCommand = new SqlCommand(MySQL, connection); //定义数据集对象 DataSet MyDataSet = new DataSet(); //填充数据集的数据 MyAdapter.Fill(MyDataSet,"student"); DataTable MyTable = MyDataSet.Tables["student"]; //定义一个全新的数据行对象 DataRow MyRow = MyTable.NewRow(); //定义数据行中各字段的值 MyRow["xuehao"] = "98007131"; MyRow["name"] = "mary"; MyRow["sex"] = "女"; MyRow["age"] = 18; MyRow["depart"] = "计算机"; //在内存中的表对象中添加一个新行,但此时新行的内容没有更新到数据源中 MyTable.Rows.Add(MyRow); //定义数据适配器对象的InsertCommand属性的Insert语句 MyAdapter.InsertCommand = new SqlCommand("Insert Into student Values('" + MyRow["xuehao"] + "','" + MyRow["name"] + "','" + MyRow["sex"] + "'," + MyRow["age"] + ",'" + MyRow["depart"] + "'"+")", connection); //显示Insert语句是否正常 Console.WriteLine(MyAdapter.InsertCommand.CommandText.ToString()); //调用数据适配器的Update方法,将新插入的数据更新到数据源中 MyAdapter.Update(MyDataSet, "student"); Console.WriteLine("成功更新数据库的数据!");
} catch (SqlException ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } finally { connection.Close(); Console.ReadLine(); Console.WriteLine("成功关闭到SQL Server 2005数据库的连接"); } } } //返回连接字符串的函数 static private string GetConnectionString() { return "server=(local);database=student;uid=sa;pwd=sql;"; }
} }
|
一共有 1 条评论
全国第一家虚拟主机:支持伪静态.有利于提高排名
15G全能空间年付500元/月付70元 可免费试用
5GB 独立WEB空间、5GB 企业邮箱空间、5GB MSSQL数据库
(可划分5个数据库。可独立放5个不同的站点)
IIS连接数据 500 个、500GB/月流量、共享日志文件空间
企业邮箱功能
赠送5GB 超大企业邮箱,500个Email企业邮箱用户
自动回复、自动转发、POP3、SMTP收发信、SMTP发信认证
邮件过滤、邮件拒收、邮件夹管理、邮件域管理、定制邮件数
数据库功能
支持5GB MSSQL数据库空间,5个用户数据库、Access
主机功能支持
采用安全稳定的Win2003 .net2.0 架构
支持ASP、PHP、ASP.NET、PERL等脚本、支持自定义CGI
全面支持.net2.0版本,独立的Application应用池,
支持SSI(Shtml),支持FrontPage扩展
可免费自行绑定5个域名、500个解析、500个子域名
详情咨询QQ:443933