第一步导入两个命名空间:
using.system.data
using.system.data.sqlclient
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;
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
}
检验邮件@:
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"]);
}
删除的办法:
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 第一是中间 第二是标题 第三是按钮 第四是图片