C#:使用DataSet上传到数据库

一定需要SqlCommandBuilder(sda)

SqlDataAdapter sda = new SqlDataAdapter("select * from Student", conn);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
DataRow dr = ds.Tables[0].NewRow();
dr["id"] = 2013;
dr["name"] = "刘念";
dr["course"] = "高等数学";
dr["score"] = 100;
ds.Tables[0].Rows.Add(dr);
sda.Update(ds);
ds.AcceptChanges();

Txt文件从c#上传到数据库

文件txt

_2018DB db = new _2018DB();
string path = "D:/code/2017 研究生复试/patient.txt";
  StreamRe
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的数据库 包含图书管理 借书还书 学生管理 老师管理 和数据连接的相关应用 代码有详细的解释 压缩包面也有 数据库的文件 代码设置的数据库 用户是 sa 密码是 123456 请使用的时候做相关的修改 下面给出 一部分的代码 请继续关注本资源的发布 会后面有很多实用的代码上传 using System.Windows.Forms; namespace LibraryMis { public class DatabaseAccess { /* 声明成员变量,这样这个类中的所有方法就可是使用这些变量了 */ private SqlConnection myConnection; private SqlCommand myCommand; private SqlDataAdapter myDataAdapter; private DataSet mySet = new DataSet(); /* 写该类的构造方法,该方法名要跟类名相同,无返回值 * 当new这个类时就会执行这个构造方法 */ public DatabaseAccess() { /* 获得保存连接字符串的文件名及路径 */ //获得应用程序路径 string exePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //根据路径和文件名构建FileInfo对象 string fileName = exePath + "connectionString.txt"; //建立FileInfo对象 FileInfo f = new FileInfo(fileName); //判断该文件是否存在 if (f.Exists)//文件存在 { //获得文件内容即存在文件中的连接字符串 //打开文件,获得读文件的数据流对象 StreamReader sr = f.OpenText(); //读文件到变量中 string connectionString = sr.ReadToEnd(); //关闭流 sr.Close(); //由读出的连接字符串创建Connection对象 myConnection = new SqlConnection(connectionString); //由Connection对象创建Command对象 myCommand = myConnection.CreateCommand(); //创建DataAdapter对象 myDataAdapter = new SqlDataAdapter(); myDataAdapter.SelectCommand = myCommand; //创建CommandBuilder对象 SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter); //尝试是否能够打开连接 try { myConnection.Open(); } catch (Exception ex) //打开连接出错,可能是连接字符串有问题,这调用数据库访问设置窗体来重新设置服务器名和数据库名 { MessageBox.Show("连接不到数据库LibraryMis,请在“数据库访问设置窗体中对数据库访问进行正确的设置”" + ",取消登录后重新启动图书馆管理系统!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning ); //创建 数据库访问设置窗体,并显示 FormSetDatabase fmsd = new FormSetDatabase(); fmsd.ShowDialog(); } finally { try { myConnection.Close(); } catch (Exception ex) { } } return; } else //文件不存在 { //设置默认的连接字符串 string connectionString = "server=.;database=LibraryMis;uid=sa;pwd=123456"; //把这个字符串写入文件 StreamWriter sw = new StreamWriter(fileName); sw.Write(connectionString); sw.Close(); MessageBox.Show("文件" + fileName + "不存在,已创建该文件,请重新启动图书馆管理系统","警告",MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } /*创建查询的方法,返回数据集对象DataSet,参数SelectString表示查询的Sql语句,TableName表示要查询的表名*/ public DataSet FillDataSet(string SelectString, string TableName) { myDataAdapter.SelectCommand.CommandText = SelectString;//设置查询的Sql语句 myDataAdapter.Fill(mySet,TableName); return mySet; } /*执行插入,更新,修改的操作,参数CommandString表示Sql语句*/ public void ExeCommand(string CommandString) { myCommand.CommandText = CommandString; myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning); } finally { myConnection.Close(); } } /*执行存储过程的方法,参数为Command对象*/ public void ExeStoreProcedure(SqlCommand command) { command.Connection = myConnection; myCommand = command; myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { myConnection.Close(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值