C#中的sql增加语句是什么?
2个回答
展开全部
1楼的回答是正确的
引用“insert into [表名] (字段1,字段2,……) values (值1,值2,……)”
综合表达:
连接数据库
public OleDbConnection GreatConn()
{
string strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("mdb/wxy.Mdb")+";";
OleDbConnection objConnection=new OleDbConnection (strConnection);
return objConnection;
}
添加
private void add()
{
string tjwords=this.tjwords.Text;
string tjcomm="insert into 表名(name) values ('"+tjwords+"')";
OleDbConnection conn=GreatConn();
conn.Open();
OleDbCommand comm=new OleDbCommand(tjcomm,conn);
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
}
删除
private void del()
{
string tjwords=this.tjwords.Text;
string tjcomm="delete from 表名 where name=('"+tjwords+"')";
OleDbConnection conn=GreatConn();
conn.Open();
OleDbCommand comm=new OleDbCommand(tjcomm,conn);
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
}
下面我放一个我的代码
实例代码演示:
protected void Button2_Click(object sender, EventArgs e)
{
string str = "Provider=Microsoft.Jet.OleDb.4.0;data source=C:/Users/x.Chao/Documents/Visual Studio 2005/WebSites/WebSite19/ASPNETDB09.mdb;";//数据库连接
OleDbConnection con = new OleDbConnection(str);
con.Open();
//添加数据开始
string sqlStr = "insert into tbArticle(articleTitle,articleType,articleContent,userName,source,BuildTime)"
+ "values('"
+ TextBox1.Text + "','"
+ DropDownList1.SelectedItem.Text + "','"
+ TextBox4.Text + "','"
+ TextBox2.Text + "','"
+ DropDownList2.SelectedItem.Text + "','"
+ TextBox3.Text + "')";
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
try
{
cmd.ExecuteNonQuery();
Response.Write("添加成功!");
}
catch
{
Response.Write("添加失败!");
}
//添加完成....
引用“insert into [表名] (字段1,字段2,……) values (值1,值2,……)”
综合表达:
连接数据库
public OleDbConnection GreatConn()
{
string strConnection="provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath("mdb/wxy.Mdb")+";";
OleDbConnection objConnection=new OleDbConnection (strConnection);
return objConnection;
}
添加
private void add()
{
string tjwords=this.tjwords.Text;
string tjcomm="insert into 表名(name) values ('"+tjwords+"')";
OleDbConnection conn=GreatConn();
conn.Open();
OleDbCommand comm=new OleDbCommand(tjcomm,conn);
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
}
删除
private void del()
{
string tjwords=this.tjwords.Text;
string tjcomm="delete from 表名 where name=('"+tjwords+"')";
OleDbConnection conn=GreatConn();
conn.Open();
OleDbCommand comm=new OleDbCommand(tjcomm,conn);
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
}
下面我放一个我的代码
实例代码演示:
protected void Button2_Click(object sender, EventArgs e)
{
string str = "Provider=Microsoft.Jet.OleDb.4.0;data source=C:/Users/x.Chao/Documents/Visual Studio 2005/WebSites/WebSite19/ASPNETDB09.mdb;";//数据库连接
OleDbConnection con = new OleDbConnection(str);
con.Open();
//添加数据开始
string sqlStr = "insert into tbArticle(articleTitle,articleType,articleContent,userName,source,BuildTime)"
+ "values('"
+ TextBox1.Text + "','"
+ DropDownList1.SelectedItem.Text + "','"
+ TextBox4.Text + "','"
+ TextBox2.Text + "','"
+ DropDownList2.SelectedItem.Text + "','"
+ TextBox3.Text + "')";
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
try
{
cmd.ExecuteNonQuery();
Response.Write("添加成功!");
}
catch
{
Response.Write("添加失败!");
}
//添加完成....
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询