C#连接数据库代码(基础)
来源:互联网 发布:sqlserver enterprise 编辑:程序博客网 时间:2024/05/16 00:27
<pre name="code" class="csharp"> //string connStr = "server=xx.xx.xx.xx;database=xxDB;uid=sa;password=xxx"; private static string connStr = System.Configuration.ConfigurationSettings.AppSettings["Connection"].ToString();//web.config <appSettings><add key="Connection" value="server=xx.xx.xx.xx;database=xxDB;uid=sa;password=xxx" /></appSettings> private SqlConnection conn = new SqlConnection(connStr); //增 protected void add_Click(object sender, EventArgs e) { string sqlStr = "insert into 友情链接([外链名]) values ('测试')"; execSql(sqlStr); queryTable(); } //删 protected void del_Click(object sender, EventArgs e) { string sqlStr = "delete 友情链接 where ID>9"; execSql(sqlStr); queryTable(); } //改 protected void modify_Click(object sender, EventArgs e) { string sqlStr = "update 友情链接 set 外链名='测试2' where ID>9"; execSql(sqlStr); queryTable(); } //查 protected void query_Click(object sender, EventArgs e) { queryTable(); } //查询通用 private void queryTable() { if (this.conn.State == ConnectionState.Closed) { this.conn.Open(); } else if (this.conn.State == ConnectionState.Open) { this.conn.Close(); this.conn.Open(); } else if (this.conn.State == ConnectionState.Broken) { this.conn.Close(); this.conn.Open(); } string sqlStr = "select * from 友情链接"; SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn); DataTable dt = new DataTable(); da.Fill(dt); conn.Close(); Repeater1.DataSource = dt.DefaultView; Repeater1.DataBind(); } //增删改通用 private void execSql(string sqlStr) { if (this.conn.State == ConnectionState.Closed) { this.conn.Open(); } else if (this.conn.State == ConnectionState.Open) { this.conn.Close(); this.conn.Open(); } else if (this.conn.State == ConnectionState.Broken) { this.conn.Close(); this.conn.Open(); } SqlCommand cmd = new SqlCommand(sqlStr, conn); int res = cmd.ExecuteNonQuery(); conn.Close(); }
0 0
- C#连接数据库代码(基础)
- C#基础 连接数据库
- c#连接数据库代码
- C#连接数据库的代码
- object_PDO基础连接数据库代码
- c#连接MDB,SQL数据库代码
- C#连接数据库几段代码-加注
- C#连接六类数据库的代码
- c#连接数据库+代码全辑
- asp.net c# 连接各种数据库代码
- C# 连接ACCESS数据库代码实例
- c#连接六类数据库代码集
- C#连接Access2007数据库代码实例完整版
- C#连接六类数据库的代码
- c#连接各种数据库的代码
- C#连接Access数据库实例代码讲解
- C#代码连接SQL Server数据库
- C#连接Access2007数据库代码实例完整版
- EF(EntityFramework) 的 CodeFirst 使用指南二(基本使用)
- 解决Chrome插件安装时出现的“程序包无效”问题
- NSRULConnection网络应用
- 打造自己的vim
- 笔记:继承设计的技巧
- C#连接数据库代码(基础)
- [数位DP] HDU4734 F(x)
- 结构体指针之 段错误 详解(segmentation fault)
- Hbase存储数据结构
- 指针和数组的不同
- 网络-NSURLSession应用和原理
- 位运算:二进制中1的个数
- 字符串匹配
- Android面试题搜集