VS2015怎么样连接数据库?本人数据库是SQL2014的

.Net技术 码拜 8年前 (2016-03-05) 1553次浏览
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Security.Cryptography;
using System.Web.Configuration;
public partial class Register : System.Web.UI.Page
{
private bool doCheck(string condValue,string type=”LOGIN”)
{
bool ret = true;
string strConn = WebConfigurationManager.ConnectionStrings[“WeiBoConnectionString”].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
con.Open();//这里总是报错!
SqlCommand cmd = new SqlCommand(“SELECT * FROM W_USER WHERE userLogin=@cond_value”,con);
if (type.Equals(“URL”))
cmd.CommandText = “SELECT * FROM W_USER WHERE homeUrl=@cond_value”;
cmd.Parameters.AddWithValue(“@cond_value”, condValue);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
ret = false;
}
cmd = null;
con.Close();
con = null;
return ret;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void ButtonSubmit_Click(object sender,EventArgs e)
{
if(!doCheck(TextBoxLogin.Text))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “success”,”alert(“该登录名已存在”);”, true);
return;
}
if(!doCheck(TextBoxHomeUrl.Text,”URL”))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “success”, “alert(“个人微博地址已被占用”);”, true);
return;
}
string strConn = WebConfigurationManager.ConnectionStrings[“WeiBoConnectionString”].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand cmd = new SqlCommand(“INSERT INTO W_USER (userName,” +
“userLogin,userPassword,userSex,homeUrl,userEmail,userInfo,” +
“userAddress,registTime) values(@user_name,@user_login,@user_password,” +
“@user_sex,@home_url,@user_email,@user_info,@user_address,@regist_time)”, con);
cmd.Parameters.AddWithValue(“@user_name”, TextBoxName.Text);
cmd.Parameters.AddWithValue(“@user_login”, TextBoxLogin.Text);
SHA1CryptoServiceProvider sha1csp = new SHA1CryptoServiceProvider();
byte[] src = System.Text.Encoding.UTF8.GetBytes(TextBoxPassword.Text);
byte[] des = sha1csp.ComputeHash(src);
cmd.Parameters.AddWithValue(“@user_password”, Convert.ToBase64String(des));
cmd.Parameters.AddWithValue(“@user_sex”, RadioButtonList1.SelectedValue);
cmd.Parameters.AddWithValue(“@user_url”, TextBoxHomeUrl.Text);
cmd.Parameters.AddWithValue(“@user_email”, TextBoxEmail.Text);
cmd.Parameters.AddWithValue(“@user_info”, TextBoxInfo.Text);
cmd.Parameters.AddWithValue(“@user_address”, TextBoxAddress.Text);
cmd.Parameters.AddWithValue(“@regist_time”,DateTime.Now);
int count = cmd.ExecuteNonQuery();
if(count==1)
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “success”, “alert(“恭喜你,注册成功”);”, true);
else Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “success”, “alert(“注册失败”);”, true);
cmd = null;
con.Close();
con = null;
Response.Redirect(“Login.aspx”);
}
protected void Reset_Click(object sender, EventArgs e)
{
}
}
错误信息:
在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称能否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 – 定位指定的服务器/实例时出错)
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称能否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 – 定位指定的服务器/实例时出错)
解决方案

10

你连接字符串里设置的数据库名貌似不对

30

你可以看下这帖http://bbs.csdn.net/topics/360236136

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明VS2015怎么样连接数据库?本人数据库是SQL2014的
喜欢 (0)
[1034331897@qq.com]
分享 (0)