我有一个问题与我的asp.net项目建立连接到ms sql服务器。下面是我的aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OnlineAppSyss.aspx.cs" Inherits="SoftwareAnalysisAndDesign.SAD.OnlineAppSyss" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Online AppSyss System</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="css/style.css" />
<script src="js/index.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body bgcolor="#339966">
<div class="wrapper">
<div class="container">
<h1>Welcome to Online AppSess System</h1>
<form id="form1" runat="server">
<input type="text" id="Username" runat="server" placeholder="Username" />
<input type="text" id="Password" runat="server" placeholder="Password" />
<button type="submit" id="login-button" onserverclick="Button1_Click">Login</button>
</form>
</div>
</div>
<ul class="bg-bubbles">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
和我的aspx代码后面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareAnalysisAndDesign.SAD
{
public partial class OnlineAppSyss : System.Web.UI.Page
{
public class MSConnector
{
public String ConnectionString { get; set; }
public DataSet ExecuteQuery(String sqlStatement)
{
try
{
DataSet results = new DataSet();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
using (SqlDataAdapter da = new SqlDataAdapter(sqlStatement, conn))
{
da.Fill(results);
}
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
return results;
}
catch (Exception ex)
{
throw ex;
}
}
}
public static string query = null;
private DataSet selectedData;
private DataTable dt;
private MSConnector connector = new MSConnector();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Login();
}
public void Login()
{
//ConnectionString for accessing into MSSql
connector.ConnectionString = "SERVER=KEITH;UID=KEITH/LaurenceKeith;Password=;DATABASE=Student;";
string username = (this.Username.Value);
string password = (this.Password.Value);
if (username == "" && password == "")
{
query = "select * from Student where StudentID = 2011017997'";
query = "select * from Student where Password = 'lalbano' '";
}
}
}
}
这是我使用MSConnector类设置连接的代码
public class MSConnector
{
public String ConnectionString { get; set; }
public DataSet ExecuteQuery(String sqlStatement)
{
try
{
DataSet results = new DataSet();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
using (SqlDataAdapter da = new SqlDataAdapter(sqlStatement, conn))
{
da.Fill(results);
}
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
return results;
}
catch (Exception ex)
{
throw ex;
}
}
}
我不能检索我的数据在我的数据库中,即使我没有错误在我的代码后面。
我的连接字符串有问题吗?我没有密码在我的ms sql server虽然。这是连接字符串的正确代码吗?请帮助。
//ConnectionString for accessing into MSSql
connector.ConnectionString = "SERVER=KEITH;UID=KEITH/LaurenceKeith;Password=;DATABASE=Student;";
尝试此连接字符串,将使用Windows身份验证登录。
connector.ConnectionString = "data source=KEITH;initial catalog=Student;Integrated Security=SSPI;providerName=System.Data.SqlClient";
假设Keith是你的SQL服务器,数据库是Student