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.IO;
using System.Configuration;
namespace Iknowyourbrain
{
public partial class WebForm1 : System.Web.UI.Page
{
public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{ (Parent as TextBox).Text = string.Empty; }
else
{
foreach (Control c in Parent.Controls)
ClearControls(c);
}
}
private void ExecuteInsert(string username, string password, string age, string gender, string emailaddress)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO tblRegistration (UserName, Password, Age, Gender, Email Address) VALUES "
+ " (@UserName,@Password,@Age,@Gender,@Email Address)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[6];
param[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@Age", SqlDbType.Char, 10);
param[3] = new SqlParameter("@Gender", SqlDbType.Int, 100);
param[4] = new SqlParameter("@Email Address", SqlDbType.VarChar, 50);
param[0].Value = username;
param[1].Value = password;
param[2].Value = age;
param[3].Value = gender;
param[4].Value = emailaddress;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
public string GetConnectionString()
{
//sets the connection string from your web config file "ConnString" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//call the method to execute insert to the database
ExecuteInsert(
TxtUserName.Text,
TxtPassword.Text,
DropDownListGender.SelectedItem.Text,
TxtAge.Text, TxtEmailAddress.Text);
Response.Write("Record was successfully added!");
ClearControls(Page);
}
}
}
是我的代碼爲我的網站到目前爲止。在我web.config
文件,我有連接字符串奇怪的錯誤
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="MyConsString" connectionString="Data Source=WPHVD185022-9O0;
Initial Catalog=MyDatabase;
Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
,當我試圖做一個測試,註冊一個賬號,我得到這個錯誤:
Exception was unhandled by user code
Insert Error:A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
您是否有權限訪問SQL Server? – Melanie 2013-03-12 16:26:21
即時聯繫你是什麼意思你有認知 – Boris 2013-03-12 16:33:23
你說你已經連接。連接如何? – Melanie 2013-03-12 19:53:15