初学者求救啊!Visual studio2010怎么用代码连上SQL server 数据库呢?

ljb5460 2012-03-22 11:57:06
我想做个登录界面 有哪位大哥大姐 能帮帮我 。。谢谢了
...全文
743 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
傍山人家 2014-02-26
  • 打赏
  • 举报
回复
9楼您好,这个代码 抛出异常:未将对象引用设置到对象的实例。 行数就30行: string sqlstr = System.Configuration.ConfigurationManager.AppSettings["con"].ToString(); 有解决办法吗
01看世界 2012-03-23
  • 打赏
  • 举报
回复
中文解释塞进去了

Sqlconnection conn = new Sqlconnection(数据的连接字符串);
conn.Open();
string sqlstr ="SQL语句";
Sqlcommand cmd = new Sqlcommand(sqlstr,conn);
再用SqlDataReader
ljb5460 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 softkexin 的回复:]
Ado.net连接SqlServer的几个重要类,SqlConnection,SqlCommand,SqlDataAdapter,SqlDataReader...
[/Quote] 可以具体给个例子么?
IT-Style 2012-03-23
  • 打赏
  • 举报
回复
Ado.net连接SqlServer的几个重要类,SqlConnection,SqlCommand,SqlDataAdapter,SqlDataReader...
myhope88 2012-03-23
  • 打赏
  • 举报
回复
.net连接数据库都不多呀,虽然开发环境不一样
ljb5460 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 n_dota 的回复:]
HTML code
<table align="center" cellpadding="0" cellspacing="0" class="style1"
style="height: 189px; width: 360px">
<tr>
<td class="style4">
用……
[/Quote]
弄好了 谢谢大家啊 ~
porschev 2012-03-23
  • 打赏
  • 举报
回复

连接字符串 http://www.connectionstrings.com/

SqlConnection
n_dota 2012-03-23
  • 打赏
  • 举报
回复
<table align="center" cellpadding="0" cellspacing="0" class="style1" 
style="height: 189px; width: 360px">
<tr>
<td class="style4">
用  户  名:</td>
<td class="style3">
<asp:TextBox ID="txtUserName" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
密        码:</td>
<td class="style3">
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="150px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPwd" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
安全问题:</td>
<td class="style3">
<asp:TextBox ID="txtquestion" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtquestion" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
提示答案:</td>
<td class="style3">
<asp:TextBox ID="txtresult" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtresult" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
电子邮件:</td>
<td class="style3">
<asp:TextBox ID="txtEmail" runat="server" Width="150px"></asp:TextBox>
</td>
<td valign="middle">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="txtEmail" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
BorderStyle="Solid" BorderWidth="1px" ControlToValidate="txtEmail"
Display="Dynamic" ErrorMessage="邮件地址错误" Font-Overline="False" Font-Size="10pt"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style4">
 </td>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" />
 
<asp:Button ID="Button2" runat="server" Text="重置" onclick="Button2_Click" />
</td>
</tr>
</table>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class login : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
txtEmail.Text = "";
txtPwd.Text = "";
txtquestion.Text = "";
txtresult.Text = "";
txtUserName.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlstr = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();
SqlConnection conn = new SqlConnection(sqlstr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select count(*) from Tb_Login where UseName='"+txtUserName.Text.Trim()+"'";
int flag=int.Parse(cmd.ExecuteScalar().ToString());
if (flag > 0)
{
Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('该用户名已经存在');", true);
}
else
{
cmd.CommandText = "insert into Tb_Login(UseName,UsePassword,UserEmail,Question,UserKey) values ('" + txtUserName.Text + "','" + txtPwd.Text + "','" + txtEmail.Text + "','" + txtquestion.Text + "','" + txtresult.Text + "')";
if(cmd.ExecuteNonQuery()>0)
{
Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('用户注册成功');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(this.Parent.GetType(), "", "alert('用户注册失败');", true);
}
}
conn.Close();
}
}

tan598121925 2012-03-23
  • 打赏
  • 举报
回复
找几个登录的例子看看就明白了[Quote=引用 7 楼 lee3217813 的回复:]

这··,还是自己先学吧,一点不知道的话就不是几句话能说明白的
[/Quote]
lee3217813 2012-03-23
  • 打赏
  • 举报
回复
这··,还是自己先学吧,一点不知道的话就不是几句话能说明白的
我爱小土豆 2012-03-23
  • 打赏
  • 举报
回复
data source=.;initial catalog=数据库名;integrated security=true

这是集成验证,数据库没有密码的情况下。
壁橱里的老王 2012-03-23
  • 打赏
  • 举报
回复

SqlConnectionStringBuilder b = new SqlConnectionStringBuilder();
b.DataSource = @".";//本地数据库
b.IntegratedSecurity = true;
b.InitialCatalog = "northwind";//数据库名
String conStr = b.ConnectionString;
SqlConnection con = new SqlConnection(conStr);
string sql=""//sql 脚本
sqlcommand cmd= new sqlcommand(sql,con);
con.open();
int result=cmd.executescalar();
cmd.close
if(result)//进行登录处理
response.redirect()
else
alert("<script>**failed**</script>")

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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