自学笔记

connection对象

第一步导入两个命名空间:

using.system.data

using.system.data.sqlclient

command对象


第二章内容


static void Main(string[] args)

        {

            //定义数据库连接字符串

            string connstring="server =.; database = huawei;Integrated security = true";

            //创建连接对象

            SqlConnection employee = new SqlConnection(connstring);

            Console.WriteLine("姓名和性别");

            string name = Console.ReadLine();

            string gender = Console.ReadLine();

            //打开数据库连接

            employee.Open();

            string sql;

            //创建添加的SQL语句

            sql = string.Format("insert Employee(name,Gender) " +

                "  values ('{0} ','{1}') ", name, gender);

            //建立command对象

            SqlCommand cmd = new SqlCommand(sql, employee); //两个参数分别为 SQL语句和连接对象

            if (cmd.ExecuteNonQuery()>0)

            {

                Console.WriteLine("已打开");

            }

            else Console.WriteLine("未打开");

            //执行SQL命令,返回DataReader对象

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())

            {

                string name1 = reader["name"].ToString();

                Console.WriteLine(name1);

            }

            //关闭Date.reader对象

            reader.Close();

            employee.Close();

        }


第三章作业



DBhelper类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data;//-----5

using System.Data.SqlClient;//-----5

namespace ConsoleApplication1

{

    class DBHelper

    {

        //数据库连接字符串----5

        public static string ConnString = "server=.;database=huawei;" +

            "Integrated Security=True;";

        //数据库连接对象-----5

        public static SqlConnection Conn = null;

        //初始化数据库连接---5

        public static void InitConnection()

        {

            //如果连接对象不存在,则创建连接-----5

            if (Conn == null)

                Conn = new SqlConnection(ConnString);

            //如果连接对象关闭,则打开连接----5

            if (Conn.State == ConnectionState.Closed)

                Conn.Open();

            //如果连接中断,则重启连接------5

            if (Conn.State == ConnectionState.Broken)

            {

                Conn.Close();

                Conn.Open();

            }

        }

        //查询,获取DataReader-----总分30

        public static SqlDataReader GetDataReader(string sqlStr)//-----10

        {

            InitConnection();//-----4

            SqlCommand cmd = new SqlCommand(sqlStr, Conn);//-----4

            //CommandBehavior.CloseConnection 命令行为:当DataReader对象被关闭时,自动关闭占用的连接对象

            return cmd.ExecuteReader(CommandBehavior.CloseConnection);//-----12

        }

        //查询,获取DataTable

        public static DataTable GetDataTable(string sqlStr)

        {

            InitConnection();

            DataTable table = new DataTable();

            SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);

            dap.Fill(table);

            Conn.Close();

            return table;

        }

        //增删改-----总分30

        public static bool ExecuteNonQuery(string sqlStr)//-----10

        {

            InitConnection();//-----4

            SqlCommand cmd = new SqlCommand(sqlStr, Conn);//-----4

            int result = cmd.ExecuteNonQuery();//-----4

            Conn.Close();//-----4

            return result > 0;//-----4

        }

        //执行集合函数-----总分30

        public static object ExecuteScalar(string sqlStr)//-----10

        {

            InitConnection();//-----4

            SqlCommand cmd = new SqlCommand(sqlStr, Conn);//-----4

            object result = cmd.ExecuteScalar();//-----4

            Conn.Close();//-----4

            return result;//-----4

        }

    }

}


窗口1(主窗体)

Form2 form2=new Form2();

form2.form1=this;


窗口2 

public Form form1;


窗体3

Form3 form3=new Form3();

form3.mdiparent=form1;

显示dataview数据


模糊查询


模糊查询的另一种方式

5.20日记录新的dbhelper

public static DataTable Select(string sql)

        {

            DataTable dt = new DataTable();

            SqlDataAdapter dap = new SqlDataAdapter(sql, strConn);

            dap.Fill(dt);

            return dt;

        }

pubulic static bool noselect (string sql){

sqlconnection conn=new sqlconnection(connstring);

sqlcommand cmd=new sqlcommand(sql,conn);

return cmd.executenonquery()>0



}


//窗体加载时,绑定下拉列表 //从数据库中获取歌手的类型


load事件:光标直接在查询的地方


检验邮件@:

int hh=tbemail.text;

tbemail.text.indexof("@");

if  (hh<0)


if (textBox6.Text.IndexOf("@") != textBox6.Text.LastIndexOf("@") || textBox6.Text.IndexOf("@")==-1)

{

MessageBox.Show("邮箱格式输入错误!","错误:",MessageBoxButtons.OK);

}



获取每一行的数据!!!

if(dt.rows.count>0) 再进行上面步骤




显示下拉框里面的东西,从数据库中提取!


foreach (DataRow item in dt.Rows)

            {

                this.cbb_type.Items.Add(item["SingerType"]);

            }


displaymember是控件里面显示出来的,valuemember是数据库里面的





DataGridView很详细的用法

删除的办法:

DataGridViewRow dr = dataGridView1.CurrentRow;

            string id = dr.Cells[0].Value.ToString();

            string sql = string.Format(@"delete from student where name='{0}'", id);

            if (DBHelper.noselect(sql))

            {

                MessageBox.Show("删除成功");

            }

            //DataGridViewRow dr = dataGridView1.CurrentRow;

            //string s = dr.Cells[0].Value.ToString();

            //string sql = string.Format("delete student where name = '{0}'", s);

            //if (DBHelper.noselect(sql))

            //{

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

            //    string sql2 = string.Format("select * from student");

            //    DataTable dt = DBHelper.select(sql2);

            //    dataGridView1.AutoGenerateColumns = false;

            //    dataGridView1.DataSource = dt;

            //}

            //else

            //    MessageBox.Show("删除失败!");

        }

按确认是否删除

mbs 第一是中间  第二是标题  第三是按钮  第四是图片


datatable与dataset的区别

DataSet & DataTable &DataRow 深入浅出

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 导入两个命名空间 using system.data; using system.data.sqlclient; ...
    程序媛_阅读 742评论 0 0
  • 该类为本人项目使用中的oracle连接工具类 1.先引入OraOps10.dll 2.将该类加入项目中 dll及源...
    羚羊藏历阅读 1,303评论 0 0
  • #DBHelp ``` public class DBHelper { //数据库连接字符串 private...
    暗组夏天阅读 2,539评论 0 1
  • using System; using System.Collections.Generic; using Sys...
    程序媛_阅读 282评论 0 0
  • using System; using System.Collections.Generic; using Sys...
    程序媛_阅读 196评论 0 0