一:数据库连接代码:

SqlConnection objSqlConnection = new SqlConnection ("server = 127.0.0.1;uid = sa; pwd =;database =test");

objSqlConnection.Open();

二:数据库的添加记录代码:

            int i = 0;

          string s1 = "", s2 = "";

          i = Convert.ToInt16(textBox1.Text);

              s1 = textBox2.Text;

          s2 = textBox3.Text;

SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.1;uid = sa; pwd =;database =test");

            objSqlConnection.Open();

            

            MessageBox.Show("数据库连接成功", "好");

try

               {

                  SqlCommand sqlcom = new SqlCommand("insert into info(id,name,sex) values( " + i + ",'" + s1 + "','" + s2 + "')", objSqlConnection);

                  sqlcom.ExecuteNonQuery();

                   MessageBox.Show("添加成功!", "啊");

               }

              catch (Exception a)

               {

                   MessageBox.Show(a.ToString());

               }

            MessageBox.Show("添加成功!", "啊");

}

三:数据库的修改代码:

            int i = 0;

            string s1 = "", s2 = "";

            s1 = textBox2.Text;

            s2 = textBox3.Text;

            if (textBox1.Text.Length == 0)

                i = 0;

            else

                i = Convert.ToInt32(textBox1.Text);

SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.1;uid = sa; pwd =;database =test");

            objSqlConnection.Open();

            MessageBox.Show("数据库连接成功", "好");

            try

            {

                SqlCommand sqlcom = new SqlCommand("update info set name='"+s1+"',sex='"+s2+"'"+"where id=" + i, objSqlConnection);

                sqlcom.ExecuteNonQuery();

                MessageBox.Show("修改成功!", "啊");

                objSqlConnection.Close();

            }

            catch (Exception a)

            {

                MessageBox.Show(a.ToString());

            }

四:数据库的删除代码:

            int i = 0;

            string s1 = "", s2 = "";

            s1 = textBox2.Text;

            s2 = textBox3.Text;

            if(textBox1.Text.Length==0)

                i=0;

            else

                 i = Convert.ToInt16(textBox1.Text);

            SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.1;uid = sa; pwd =;database =test");            

            objSqlConnection.Open();

            MessageBox.Show("数据库连接成功", "好");

            try

            {

                SqlCommand sqlcom = new SqlCommand("delete from info where id="+i, objSqlConnection);

sqlcom.ExecuteNonQuery();

MessageBox.Show("删除成功!", "啊");

objSqlConnection.Close();

            }

            catch (Exception a)

            {

                MessageBox.Show(a.ToString());

            }

五:数据库的查询代码:

            1.类开始:

                  DataTable dt1 = new DataTable();

                  SqlDataAdapter da1 = new SqlDataAdapter();

2.按钮代码:

            int i = 0,n=0;

            string s1 = "", s2 = "";

            s1 = textBox2.Text;

            s2 = textBox3.Text;

           if (textBox1.Text.Length == 0)

                i = 0;

            else

                i = Convert.ToInt32(textBox1.Text);

            SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.1;uid = sa; pwd =;database =test");

            objSqlConnection.Open();

            MessageBox.Show("数据库连接成功", "好");

string query = "SELECT * from info where id="+i;

DataSet objDataSet = new DataSet();

SqlDataAdapter obj = new SqlDataAdapter();    

      

            obj.SelectCommand = new SqlCommand(query, objSqlConnection);

obj.Fill(objDataSet, "info");

SqlCommand objSqlCommand = new SqlCommand(query, objSqlConnection);

SqlDataReader objSqlReader = objSqlCommand.ExecuteReader();

while (objSqlReader.Read())

                {

                    n += 1;

                    MessageBox.Show("编号: " + objSqlReader.Getvalue(0) + "  姓名:" + objSqlReader.Getvalue(1) + "  性别" + objSqlReader.Getvalue(2));

                }

                if (n == 0)

                    MessageBox.Show("数据库中没有这样的记录!");

六:数据库的查询代码:

       int i = 0;

           // int n = 0;

            string s1 = "", s2 = "";

            string sql;

            s1 = textBox2.Text;

            s2 = textBox3.Text;

if (textBox1.Text.Length == 0)

            {

                i = 0;

                

            }

            else

                i = Convert.ToInt32(textBox1.Text);

           SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.1;uid = sa; pwd =;database =test");          

            objSqlConnection.Open();

            MessageBox.Show("数据库连接成功", "好");

string query = "SELECT * from info where id="+i;

if(i==0)

                  sql = "select * from info ";

            else

                  sql = "select * from info where id=" + i;

da1 = new SqlDataAdapter(sql, objSqlConnection);

dt1.Clear();

            da1.Fill(dt1);

            dataGridView1.DataSource = dt1;

数据库的封装类代码:

class DBClass

    {

public void dbclass(string  sql)

        {

            try

            {

                SqlConnection sqlcon = new SqlConnection("server = 127.0.0.1;uid = sa; pwd =;database =test");

                sqlcon.Open();

            

                SqlTransaction objt = sqlcon.BeginTransaction();  //事物开始

              

                SqlCommand sqlcom = new SqlCommand(sql, sqlcon);

                

                sqlcom.Transaction = objt; //将Command 对象设置为事物处理的对象

            

                sqlcom.ExecuteNonQuery();

                objt.Commit();         //提交事物

                sqlcon.Close();

            }

            catch (Exception a)

            {

                MessageBox.Show(a.ToString());

            }

          

        }

    }

--db2 数据库连接代码:

string strcon = "Provider = IBMDADB2; Data Source=hfzd;User Id=db2admin;Password=db2admin";

            //string sql = "select * from ADMINISTRATOR.HFZD";

string sql = "delete from ADMINISTRATOR.HFZD where ID=1";

OleDbConnection olecon = new OleDbConnection(strcon);

            olecon.Open();

            MessageBox.Show("数据库已连接上");

             dt.Clear();

            da = new OleDbDataAdapter(sql, olecon);

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            olecon.Close();

C#数据库连接操作大全的更多相关文章

  1. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

  2. java日期操作大全

    摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http ...

  3. PHP数组操作大全

    <?php /** * File: phpstudy : array_test.php * Created by PhpStorm. * User: IhMfLy Pheonix@jtv-070 ...

  4. Java 文件操作大全

    Java 文件操作大全 //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPat ...

  5. SQL语句操作大全

    SQL语句操作大全   本文分为以下六个部分: 基础部分 提升部分 技巧部分 数据开发–经典部分 SQL Server基本函数部分 常识部分 一.基础 1.说明:创建数据库CREATE DATABAS ...

  6. c#数据库操作大全

    原文:c#数据库操作大全 1.提取单条记录 //using System.Data; //using System.Data.SqlClient;  using (SqlConnection cn = ...

  7. MATLAB命令大全和矩阵操作大全

    转载自: http://blog.csdn.net/dengjianqiang2011/article/details/8753807 MATLAB矩阵操作大全 一.矩阵的表示在MATLAB中创建矩阵 ...

  8. SQLite3命令操作大全

    SQLite3命令操作大全 SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.ql ...

  9. DotNet,PHP,Java的数据库连接代码大全(带演示代码)

    C#数据库连接字符串 Web.config文件 <connectionStrings> <!--SQLServer数据库连接--> <add name="con ...

随机推荐

  1. 带有进度条的WebView

    带有进度条的WebView 本篇继于WebView的使用 效果图 自定义一个带有进度条的WebView package com.kongqw.kbox.view; import android.con ...

  2. AsyncTask还要知道的一些知识

    在之前的博客中,对AsyncTask做过详细分析,而且也以小案例的形式,介绍如何基本的使用它.今天再来探讨它,更多的认识,尤其在面试中以下面方式回答,可能印象分更多一些. 面试题:讲一讲您对Async ...

  3. Ajax+Struts2实现验证码验证功能

    ---------------------------------------------------------------------------------------------------- ...

  4. springMVC源码分析--HandlerAdapter(一)

    HandlerAdapter的功能实际就是执行我们的具体的Controller.Servlet或者HttpRequestHandler中的方法. 类结构如下:

  5. 网络爬虫框架Scrapy简介

    作者: 黄进(QQ:7149101) 一. 网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本:它是一个自动提取网页的程序,它为搜索引擎从万维 ...

  6. 在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天。要求尽早聚会

    在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会.骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天.要求尽早聚会 ...

  7. EBS业务学习之应付管理

    应付款系统是供应链管理的最后一个环节,它使公司能够支付供应商提供的货物和服务的费用.供应链管理的目标是保持低库存量但又有充足的存货以满足要求,仓库中的库存就等于钱,因此,应付款管理的目标是尽可能地推迟 ...

  8. EBS库存(INV)模块常用表

     select * from org_organization_definitions库存组织 select * from mtl_parameters组织参数 select * from mtl ...

  9. Jetty 嵌入式启动官方完整教程

    网上太多了,不如直接看官方的这个全面. http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty 入门地址: http://wiki.eclipse ...

  10. 使用 symbolicatecrash 解析崩溃堆栈

    一.先引用一位牛人的博贴: xcode解crashlog工具symbolicatecrash的使用 在xocde编译app的时候会同时生成一个以dsym(该app对应的符号表)作为后缀的文件,每个ap ...