关于VS2008下删除数据库数据按钮代码

关于VS2008下删除数据库数据按钮代码,第1张

///删除数据库

private void delete_Click(object sender, EventArgs e)

{

//数据库连接字符串

string connString = "DAta Source=192.168.1.102Initial Catalog=masterUser ID=saPassword=sa"

//创建新连接

SqlConnection connection = new SqlConnection(connString)

//删除数据库SQL

string sql = "drop database [User]"

try

{

connection.Open()

SqlCommand command = new SqlCommand(sql, connection)

command.ExecuteNonQuery()

}

catch (Exception ex)

{

MessageBox.Show(ex.Message)

}

finally

{

connection.Close()

}

}

///关闭确认

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)

{

DialogResult result = new DialogResult()

result = MessageBox.Show("确定要退出系统吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

if (result == DialogResult.No)

{

e.Cancel = true

return

}

else

{

Process.GetCurrentProcess().Kill()

}

}

///更新或删除数据库

private void exectu_Click(object sender, EventArgs e)

{

//数据库连接字符串

string connString = "DAta Source=192.168.1.102Initial Catalog=UserUser ID=saPassword=sa"//指定User数据库

//创建新连接

SqlConnection connection = new SqlConnection(connString)

//删除数据库user库中DB_User表中ID为1的行的SQL

string delSql = "delete from DB_User where ID=1"

//插入一条数据给DB_User表的SQL

string insertSql = "insert into DB_User values(?,?,?,?)"// ?号是指定列中的数值,自增列不用给值,非空列一定要给值

try

{

connection.Open()

SqlCommand command = new SqlCommand(delSql/insertSql, connection)//这里可以指定删除SQL或者插入SQL

command.ExecuteNonQuery()

}

catch (Exception ex)

{

MessageBox.Show(ex.Message)

}

finally

{

connection.Close()

}

}

以users表为例,有三个字段,自增长的编号id,int类型;名称name,nvarchar类型,密码pwd,nvarchar类型

首先在vs2005中引入using System.Data.SqlClient命名空间

/// <summary>

/// 增加

/// </summary>

/// <param name="name">姓名</param>

/// <param name="pwd">密码</param>

/// <returns></returns>

public int Insert(string name,string pwd)

{

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.

conn.Open()

string sql = "insert into users(name,pwd) values(@name,@pwd)"

SqlCommand cmd = new SqlCommand(sql,conn)

SqlParameter parn = new SqlParameter("@name",name)

cmd.Parameters.Add(parn)

SqlParameter parp = new SqlParameter("@pwd", pwd)

cmd.Parameters.Add(parn)

int result = cmd.ExecuteNonQuery()//result接收受影响行数,也就是说result大于0的话表示添加成功

conn.Close()

cmd.Dispose()

return result

}

/// <summary>

/// 删除

/// </summary>

/// <param name="name">姓名</param>

/// <param name="pwd">密码</param>

/// <returns></returns>

public int Update(int id)

{

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.

conn.Open()

string sql = "delete from users where id=@id"

SqlCommand cmd = new SqlCommand(sql, conn)

SqlParameter parn = new SqlParameter("@id", id)

cmd.Parameters.Add(parn)

int result = cmd.ExecuteNonQuery()//result接收受影响行数,也就是说result大于0的话表示删除成功

conn.Close()

cmd.Dispose()

return result

}

/// <summary>

/// 修改

/// </summary>

/// <param name="name">姓名</param>

/// <param name="pwd">密码</param>

/// <returns></returns>

public int Insert(string name, string pwd,int id)

{

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.

conn.Open()

string sql = "update users set name=@name,pwd=@pwd where id=@id"

SqlCommand cmd = new SqlCommand(sql, conn)

SqlParameter parn = new SqlParameter("@name", name)

cmd.Parameters.Add(parn)

SqlParameter parp = new SqlParameter("@pwd", pwd)

cmd.Parameters.Add(parn)

SqlParameter pari = new SqlParameter("@id", id)

cmd.Parameters.Add(pari)

int result = cmd.ExecuteNonQuery()//result接收受影响行数,也就是说result大于0的话表示修改成功

conn.Close()

cmd.Dispose()

return result

}

/// <summary>

/// 查询

/// </summary>

/// <returns></returns>

public DataTable Select()

{

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.

conn.Open()

string sql = "select * from users"

SqlCommand cmd = new SqlCommand(sql, conn)

SqlDataAdapter sda = new SqlDataAdapter(cmd)

DataTable dt = new DataTable()

sda.Fill(dt)

conn.Close()

cmd.Dispose()

return dt

}

方法写好后,下面举一个查询的例子,在form窗体中拖一个DataGridView,然后在Load方法中

private void Form1_Load(object sender, EventArgs e)

{

dataGridView1.DataSource = Select()

}

这样一运行,DataGridView中就会显示数据了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6812054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存