如何用C#语言编写对数据库的添改删查?
如题:如何用C#语言编写ADO.NET对数据库的添改删查?请问用什么方法.!麻烦写的详细点..!关键把步骤列出来.?建立连接字符串对象以后,下面该用什么语句进行数据库的增...
如题:如何用C#语言编写ADO.NET对数据库的添改删查?
请问用什么方法.!
麻烦写的详细点..!
关键把步骤列出来.?
建立连接字符串对象以后 ,下面该用什么语句进行数据库的增删改查的操作.! 展开
请问用什么方法.!
麻烦写的详细点..!
关键把步骤列出来.?
建立连接字符串对象以后 ,下面该用什么语句进行数据库的增删改查的操作.! 展开
展开全部
比如MSSQL:
//建立数据库连接
SqlConnection SqlConn = new SqlConnection("连接字符串");
//建立SqlCommand
SqlCommand SqlCmd = new SqlCommand("查询语句/存储过程名称", SqlConn);
//指定执行超时时间(毫秒)
SqlCmd.CommandTimeout = 0;
//添加参数
SqlCmd.Parameters.Add("@DateStart", SqlDbType.NVarChar, 25).Value = StartDate;
//指定命令类型Text/存储过程
SqlCmd.CommandType = CommandType.StoredProcedure;
//打开连接
if (SqlConn.State == ConnectionState.Closed) SqlConn.Open();
//执行命令
查询
返回数据集
SqlDataReade DataReader = SqlCmd.ExecuteReader();
返回第一行第一列
SqlCmd.ExecuteScalar();
DataSet。。。自己去查查吧
更新,删除
SqlCmd.ExecuteNonQuery();
关闭连接
if (SqlConn.State == ConnectionState.Open) SqlConn.Close();
//释放SqlCommand
SqlCmd.Dispose();
//释放SqlConnection
SqlConn.Dispose();
sql语句就不用我给你说了吧
//建立数据库连接
SqlConnection SqlConn = new SqlConnection("连接字符串");
//建立SqlCommand
SqlCommand SqlCmd = new SqlCommand("查询语句/存储过程名称", SqlConn);
//指定执行超时时间(毫秒)
SqlCmd.CommandTimeout = 0;
//添加参数
SqlCmd.Parameters.Add("@DateStart", SqlDbType.NVarChar, 25).Value = StartDate;
//指定命令类型Text/存储过程
SqlCmd.CommandType = CommandType.StoredProcedure;
//打开连接
if (SqlConn.State == ConnectionState.Closed) SqlConn.Open();
//执行命令
查询
返回数据集
SqlDataReade DataReader = SqlCmd.ExecuteReader();
返回第一行第一列
SqlCmd.ExecuteScalar();
DataSet。。。自己去查查吧
更新,删除
SqlCmd.ExecuteNonQuery();
关闭连接
if (SqlConn.State == ConnectionState.Open) SqlConn.Close();
//释放SqlCommand
SqlCmd.Dispose();
//释放SqlConnection
SqlConn.Dispose();
sql语句就不用我给你说了吧
展开全部
public class classData
{
public static SqlDataSource createDataSource(string tables)
{
//声明一个SqlDataSource对象并在结尾返回此对象
SqlDataSource sqlds = new SqlDataSource();
//设置SqlDataSource对象的连接字符串为DB类中的connstring
sqlds.ConnectionString = DB.conn;
sqlds.SelectCommand = "select * from " + tables;
//编辑命令
sqlds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlds.UpdateCommand = "sp_updateAcademe";
sqlds.UpdateParameters.Add(new Parameter("Remarks", System.TypeCode.String));
sqlds.UpdateParameters.Add(new Parameter("Name", System.TypeCode.String));
sqlds.UpdateParameters.Add(new Parameter("ID", System.TypeCode.String));
//插入行命令
sqlds.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlds.InsertCommand = "sp_insertAcademe";
sqlds.InsertParameters.Add(new Parameter("ID", System.TypeCode.String));
sqlds.InsertParameters.Add(new Parameter("Name", System.TypeCode.String));
sqlds.InsertParameters.Add(new Parameter("Remarks", System.TypeCode.String));
//删除行命令
sqlds.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlds.DeleteCommand = "sp_deleteAcademe";
sqlds.DeleteParameters.Add(new Parameter("ID", System.TypeCode.String));
return sqlds;
}
cs 页面:
SqlDataSource sqlds = classData.createDataSource("Academe");
protected void Page_Load(object sender, EventArgs e)
{
sqlds.ID = "sqlds";
Panel1.Controls.Add(sqlds);
if (!IsPostBack)
{
GridView1.DataSourceID = "sqlds";
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.AllowSorting = true;
}
}
{
public static SqlDataSource createDataSource(string tables)
{
//声明一个SqlDataSource对象并在结尾返回此对象
SqlDataSource sqlds = new SqlDataSource();
//设置SqlDataSource对象的连接字符串为DB类中的connstring
sqlds.ConnectionString = DB.conn;
sqlds.SelectCommand = "select * from " + tables;
//编辑命令
sqlds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlds.UpdateCommand = "sp_updateAcademe";
sqlds.UpdateParameters.Add(new Parameter("Remarks", System.TypeCode.String));
sqlds.UpdateParameters.Add(new Parameter("Name", System.TypeCode.String));
sqlds.UpdateParameters.Add(new Parameter("ID", System.TypeCode.String));
//插入行命令
sqlds.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlds.InsertCommand = "sp_insertAcademe";
sqlds.InsertParameters.Add(new Parameter("ID", System.TypeCode.String));
sqlds.InsertParameters.Add(new Parameter("Name", System.TypeCode.String));
sqlds.InsertParameters.Add(new Parameter("Remarks", System.TypeCode.String));
//删除行命令
sqlds.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlds.DeleteCommand = "sp_deleteAcademe";
sqlds.DeleteParameters.Add(new Parameter("ID", System.TypeCode.String));
return sqlds;
}
cs 页面:
SqlDataSource sqlds = classData.createDataSource("Academe");
protected void Page_Load(object sender, EventArgs e)
{
sqlds.ID = "sqlds";
Panel1.Controls.Add(sqlds);
if (!IsPostBack)
{
GridView1.DataSourceID = "sqlds";
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.AllowSorting = true;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//导入命名空间
using System.Data;
using System.Data.SqlClient;
namespace ConnectionSql
{
public partial class FrmConnection : Form
{
public FrmConnection()
{
InitializeComponent();
}
private void FrmConnection_Load(object sender, EventArgs e)
{
}
//添加数据
private void btnInsert_Click(object sender, EventArgs e)
{
try {
//建立连接
SqlConnection sc = new SqlConnection("server=.;database=accp;uid=sa;password=sa");
//打开连接
sc.Open();
string sex = "";
if (radMale.Checked)
{
sex = "男";
}
else
sex = "女";
string sql = "insert into stuInfo values('"+this.txtName.Text
+"',"+txtAge.Text+",'"+sex+"')";
//执行sql语句
SqlCommand scom = new SqlCommand(sql,sc);
scom.ExecuteNonQuery();
MessageBox.Show("添加成功!");
//关闭数据库
sc.Close();
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
//删除数据
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection("server=.;database=accp;uid=sa;password=sa");
sc.Open();
string sql = "delete from stuInfo where sname='" + txtName.Text + "'";
SqlCommand scom = new SqlCommand(sql, sc);
scom.ExecuteNonQuery();
MessageBox.Show("删除成功!");
sc.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
//更新数据
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection("server=.;uid=sa;password=sa;database=accp");
sc.Open();
string sql = "update stuInfo set sAge=" + txtAge.Text + " where sName='" + txtName.Text + "'";
//MessageBox.Show(sql);
SqlCommand scom = new SqlCommand(sql, sc);
scom.ExecuteNonQuery();
MessageBox.Show("修改成功!");
sc.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
//查询数据
private void btnSelect_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection("server =.;database=accp;uid=sa;password=sa");
sc.Open();
string sql = "select * from stuInfo where sName='" + txtName.Text + "'";
SqlCommand scom = new SqlCommand(sql, sc);
SqlDataReader sdr = scom.ExecuteReader();
string sex = "";
while (sdr.Read())
{
txtAge.Text = sdr.GetValue(1).ToString();
sex = sdr.GetValue(2).ToString();
if (sex == "男")
{
radMale.Checked = true;
}
else
radFeMale.Checked = true;
}
sc.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
*****************************************************************************************************************************附:原数据库
create database accp
go
use accp
go
create table stuInfo
(
sName nvarchar(10) ,
sAge int ,
sSex char(2)
)
go
insert into stuInfo
select '张三',20,'男' union
select '李四',19,'女' union
select '王五',30,'男'
go
select * from stuInfo
go
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//导入命名空间
using System.Data;
using System.Data.SqlClient;
namespace ConnectionSql
{
public partial class FrmConnection : Form
{
public FrmConnection()
{
InitializeComponent();
}
private void FrmConnection_Load(object sender, EventArgs e)
{
}
//添加数据
private void btnInsert_Click(object sender, EventArgs e)
{
try {
//建立连接
SqlConnection sc = new SqlConnection("server=.;database=accp;uid=sa;password=sa");
//打开连接
sc.Open();
string sex = "";
if (radMale.Checked)
{
sex = "男";
}
else
sex = "女";
string sql = "insert into stuInfo values('"+this.txtName.Text
+"',"+txtAge.Text+",'"+sex+"')";
//执行sql语句
SqlCommand scom = new SqlCommand(sql,sc);
scom.ExecuteNonQuery();
MessageBox.Show("添加成功!");
//关闭数据库
sc.Close();
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
//删除数据
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection("server=.;database=accp;uid=sa;password=sa");
sc.Open();
string sql = "delete from stuInfo where sname='" + txtName.Text + "'";
SqlCommand scom = new SqlCommand(sql, sc);
scom.ExecuteNonQuery();
MessageBox.Show("删除成功!");
sc.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
//更新数据
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection("server=.;uid=sa;password=sa;database=accp");
sc.Open();
string sql = "update stuInfo set sAge=" + txtAge.Text + " where sName='" + txtName.Text + "'";
//MessageBox.Show(sql);
SqlCommand scom = new SqlCommand(sql, sc);
scom.ExecuteNonQuery();
MessageBox.Show("修改成功!");
sc.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
//查询数据
private void btnSelect_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection("server =.;database=accp;uid=sa;password=sa");
sc.Open();
string sql = "select * from stuInfo where sName='" + txtName.Text + "'";
SqlCommand scom = new SqlCommand(sql, sc);
SqlDataReader sdr = scom.ExecuteReader();
string sex = "";
while (sdr.Read())
{
txtAge.Text = sdr.GetValue(1).ToString();
sex = sdr.GetValue(2).ToString();
if (sex == "男")
{
radMale.Checked = true;
}
else
radFeMale.Checked = true;
}
sc.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
*****************************************************************************************************************************附:原数据库
create database accp
go
use accp
go
create table stuInfo
(
sName nvarchar(10) ,
sAge int ,
sSex char(2)
)
go
insert into stuInfo
select '张三',20,'男' union
select '李四',19,'女' union
select '王五',30,'男'
go
select * from stuInfo
go
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sqlcommand=con.createcommand();
con.commandtxt="DELETE from 表名 where 条件"//删除
con.commandtxt="update 表名 set 字段名=值"//修改
con.commandtxt="insert into 表名(字段名)“//增加
con.commandtxt="DELETE from 表名 where 条件"//删除
con.commandtxt="update 表名 set 字段名=值"//修改
con.commandtxt="insert into 表名(字段名)“//增加
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也是刚学的,就知道可以通过gridview操作数据库,新建一个gridview,然后选择数据源,默认的话就会出现Sqldatasource1,可以通过Sqldatasource1.insertcommand = 'SQL语句'进行增加,updatecommand进行修改,deletecommand进行删除,查询的话gridview本身就可以输入查询语句。最后记得加1个相应的如Sqldatasource.update()来进行数据库修改操作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如今这个时候,还需要一步一步的Open数据库吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询