C#的基本DB操作之【插入】
2009-04-28 09:47
369 查看
经常用到所以记录下来,免得找找麻烦 ヘ(-_-ヘ フフフフフ
瞎编了个数据库和表,反正可以编译通过,呵呵~
具体就不介绍了,同道中人,不点亦明。
关于config文件中DB链接的存储方法,参考这里
/article/6991561.html
/// <summary>
/// 示例代码(write by 阿米巴原虫~)
/// </summary>
/// <param name="studentID">学生号</param>
/// <param name="studentName">学生姓名</param>
private void DBInsert(string studentID, string studentName)
{
SqlConnection connection = null;
SqlCommand command = null;
//建立一个进程,当插入失败的时候可以回滚
SqlTransaction transaction = null;
try
{
string commText = string.Empty;
//调用config文件内的DB链接字符串
connection = new SqlConnection(ConfigurationManager.AppSettings["DB.ConnectionString"]);
connection.Open();
command = connection.CreateCommand();
transaction = connection.BeginTransaction();
command.Transaction = transaction;
commText += "insert into <DBName>.<schema>.<TableName>";
commText += "(student_id,student_name,last_update)";
commText += "select @studentID,@studentName,@lastUpdate";
command.CommandText = commText;
command.Parameters.Add(new SqlParameter("@studentID", "<字段1>"));
command.Parameters.Add(new SqlParameter("@studentName", "<字段2>"));
command.Parameters.Add(new SqlParameter("@lastUpdate", System.DateTime.Now.ToString()));
//返回插入的行数
int insertCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception e)
{
//发生错误则回滚
if (transaction != null)
{
transaction.Rollback();
}
//将错误抛向上一层
throw e;
}
//最后万万不能忘记的,关闭连接
finally
{
if (connection != null)
{
connection.Close();
}
}
}
瞎编了个数据库和表,反正可以编译通过,呵呵~
具体就不介绍了,同道中人,不点亦明。
关于config文件中DB链接的存储方法,参考这里
/article/6991561.html
/// <summary>
/// 示例代码(write by 阿米巴原虫~)
/// </summary>
/// <param name="studentID">学生号</param>
/// <param name="studentName">学生姓名</param>
private void DBInsert(string studentID, string studentName)
{
SqlConnection connection = null;
SqlCommand command = null;
//建立一个进程,当插入失败的时候可以回滚
SqlTransaction transaction = null;
try
{
string commText = string.Empty;
//调用config文件内的DB链接字符串
connection = new SqlConnection(ConfigurationManager.AppSettings["DB.ConnectionString"]);
connection.Open();
command = connection.CreateCommand();
transaction = connection.BeginTransaction();
command.Transaction = transaction;
commText += "insert into <DBName>.<schema>.<TableName>";
commText += "(student_id,student_name,last_update)";
commText += "select @studentID,@studentName,@lastUpdate";
command.CommandText = commText;
command.Parameters.Add(new SqlParameter("@studentID", "<字段1>"));
command.Parameters.Add(new SqlParameter("@studentName", "<字段2>"));
command.Parameters.Add(new SqlParameter("@lastUpdate", System.DateTime.Now.ToString()));
//返回插入的行数
int insertCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception e)
{
//发生错误则回滚
if (transaction != null)
{
transaction.Rollback();
}
//将错误抛向上一层
throw e;
}
//最后万万不能忘记的,关闭连接
finally
{
if (connection != null)
{
connection.Close();
}
}
}
相关文章推荐
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- c#之Word基本操作----创建,打开,绘制表格,插入图片等
- 顺序表基本操作(插入、删除)
- 【c#】c#中 字符串的基本操作
- Chapter 3. SQL Server数据库、表的基本操作(insert插入、update更新、delete、truncate删除语句)
- C# ASP.NET 操作数据库的基本方法
- C# PowerPoint操作的基本用法
- mongodb的基本操作与插入文档(document)
- JDBC实现数据库的几种基本操作(查询,分页查询,根据关键字进行查询以及插入数据)
- C#编写的OLEDB数据库操作基本类
- C_线性表----单链表的基本操作(构表、插入、删除、倒序、输出)
- C# 操作在Word表格中插入新行(表格含合并行)
- 试用多态实现线性表(队列,串,堆栈),要求具备线性表的基本操作:插入,删除,测长等。
- 红黑树的基本插入和删除操作
- C#(.NET)数据访问连接、查询、插入等操作的封装类
- 程序员面试宝典(第三版)——单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置(转)
- C#中对XML文件的一些基本操作(使用DOM Tree)