求C#操作数据库的简单例子

kqyx_cn 2012-02-22 04:14:55
比如有表userinfo,有列username,passwrod,求用户注册提交的代码,也就是说怎么插入一条记录。
表单里的控件名为username,password.

谢谢。
...全文
117 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kqyx_cn 2012-02-22
  • 打赏
  • 举报
回复
if (mydr.Read())
{
Response.Write("<script>alert('用户已经存在');window.window.location.href='zhuce.aspx';</script>");
mydr.Close();

这段代码好像无效吧?
EnForGrass 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 kqyx_cn 的回复:]

引用 6 楼 a215316365 的回复:
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web……
[/Quote]
都是这个过程过来的。能帮则帮
kqyx_cn 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 a215316365 的回复:]
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;……
[/Quote]

谢谢你,用你的方法就可以了。
其他大哥可能都是高手了,不屑回答这样的问题。
EnForGrass 2012-02-22
  • 打赏
  • 举报
回复

private static string connstr = "server=.;database=mytest;uid=sa;pwd=sa";
SqlConnection conn = new SqlConnection(connstr); //创建连接
conn.Open(); //打开连接
string sql=string.Format("insert into userinfo(username,password)values ('{0}','{1}')",myusername,mypassword);
SqlCommand comm = new SqlCommand(sql,con);
int i=comm.ExecuteNonQuery();
if(i>0)
{
MessageBox.Show("注册成功!");
}
else
{
MessageBox.Show("注册失败!");
}
conn.Close()

资料,MSDN上看
kqyx_cn 2012-02-22
  • 打赏
  • 举报
回复
ADO.net 上面也没有这么简单的例子。
kqyx_cn 2012-02-22
  • 打赏
  • 举报
回复
连接数据库没有问题,我问的是怎么向数据库增加一条记录。
我是初学的,现在是要快速入门,我没有ADO.net的资料。
我的代码接下来应该是怎么样的?
a215316365 2012-02-22
  • 打赏
  • 举报
回复
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void bt_cancel_Click(object sender, EventArgs e)
{
Response.Redirect("zhuce.aspx");

}
protected void bt_OK_Click(object sender, EventArgs e)
{
string sqlcon = "server=localhost;database=yonghuzhuce;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(sqlcon);
conn.Open();
SqlCommand comm = new SqlCommand("select * from info where id='" + tb_id.Text.ToString() + "'", conn);
SqlDataReader mydr = comm.ExecuteReader();
if (mydr.Read())
{
Response.Write("<script>alert('用户已经存在');window.window.location.href='zhuce.aspx';</script>");
mydr.Close();

}
else
{
mydr.Close();
SqlCommand mycomm=new SqlCommand("insert into info(id,pwd,rpwd) values('"+tb_id.Text +"','"+tb_pwd.Text+"','"+tb_rpwd.Text+"')",conn);
mycomm.ExecuteNonQuery();
Session["id"] = tb_id.Text;
Session["pwd"] = tb_pwd.Text;
Response.Write("<script>alert('注册成功');window.window.location.href='login.aspx';</script>");
}
conn.Close();


}


}




代码有点简单 自己再改改 我也是个菜鸟
EnForGrass 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kqyx_cn 的回复:]

C# code

string str = "server='.';database='mytest';uid='sa';pwd='sa'"; SqlConnection con = new SqlConnection(str); //创建连接
con.Open(); ……
[/Quote]
建议你看看ADO.NET
EnForGrass 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kqyx_cn 的回复:]

C# code

string str = "server='.';database='mytest';uid='sa';pwd='sa'"; SqlConnection con = new SqlConnection(str); //创建连接
con.Open(); ……
[/Quote]
server='.'不要引号,直接是server=.后面也一样
kqyx_cn 2012-02-22
  • 打赏
  • 举报
回复

string str = "server='.';database='mytest';uid='sa';pwd='sa'"; SqlConnection con = new SqlConnection(str); //创建连接
con.Open(); //打开连接
string sql=string.Format("insert into userinfo(username,password)values ('{0}','{1}')",myusername,mypassword2);
SqlCommand comm = new SqlCommand(sql,con);


请问哪里错了?怎么让插入的动作执行?
谢谢。
  • 打赏
  • 举报
回复
ADO.NET

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧