[英]How To store data in Sql database in Microsoft Azure
當我將數據存儲在Microsoft Azure雲中時,我會遇到此問題。
這里的錯誤日志:
“ /”應用程序中的服務器錯誤。 建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:SQL網絡接口,錯誤:26-錯誤指定服務器/實例的位置)說明:在執行當前Web請求期間發生未處理的異常。 請查看堆棧跟蹤,以獲取有關錯誤及其在代碼中起源的更多信息。
異常詳細信息:System.Data.SqlClient.SqlException:建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:SQL網絡接口,錯誤:26-指定服務器/實例時出錯)
這是我的代碼:
protected void Button1_Click(object sender, EventArgs e)
{
int userId = 0;
SqlConnection con = new SqlConnection("Data Source=asimo.database.windows.net;Initial Catalog=asimo;Persist Security Info=True;User ID=veeraiyan;Password=***********");
con.Open();
SqlCommand cmd = new SqlCommand("insert into registration(username,password,email)values('" + txtusername.Text + "','" + txtpassword.Text + "','" + txtemail.Text + "')", con);
cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@password", txtpassword.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtemail.Text.Trim());
userId = cmd.ExecuteNonQuery();
con.Close();
string message = string.Empty;
switch (userId)
{
case -1:
message = "Username already exists.\\nPlease choose a different username.";
break;
case -2:
message = "Supplied email address has already been used.";
break;
default:
message = "Registration successful.\\nUser Id: " + userId.ToString();
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
查看https://www.google.com/search?q=agoogle&ie=&oe=#q=agoogle+c-sharp+azure+sql+connection+string來解決您的連接字符串問題。 然后,按照注釋中的建議進行更改:
SqlCommand cmd = new SqlCommand("insert into registration(username,password,email)values('" + txtusername.Text + "','" + txtpassword.Text + "','" + txtemail.Text + "')", con);
//change your connection string to an Azure-compatible one...
至:
SqlCommand cmd = new SqlCommand("insert into registration(username,password,email)values(@username,@password,@email, con);
在使用時,請更改:
SqlConnection con = new SqlConnection("Data Source=asimo.database.windows.net;Initial Catalog=asimo;Persist Security Info=True;User ID=veeraiyan;Password=***********");
至:
using(SqlConnection con = new SqlConnection("Data Source=asimo.database.windows.net;Initial Catalog=asimo;Persist Security Info=True;User ID=veeraiyan;Password=***********")){
//....
}
Azure Sql Server數據庫的連接字符串應寫為
string cnString = @"Data Source=tcp:asimo.database.windows.net,1433;
Database=asimo;Trusted_Connection=False;
User ID=veeraiyan@asimo;Password=***********;
Encrypt=True;";
就是說,您插入插入記錄和檢索IDENTITY列UserID的代碼未正確設置。
正確的方法如下
string cmdText = @"insert into registration
(username,password,email)
values(@username, @password, @email);
SELECT SCOPE_IDENTITY()";
using(SqlConnection con = new SqlConnection(cnString))
using(SqlCommand cmd = new SqlCommand(cmdText, con))
{
con.Open();
cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@password", txtpassword.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtemail.Text.Trim());
userId = Convert.ToInt32(cmd.ExecuteScalar());
...
請注意,要檢索UserID,您不會獲得ExecuteNonQuery的返回值。 此方法僅返回命令插入的行數。 相反,您可以將兩個命令放在一起,第一個插入記錄,第二個返回由連接生成的最后一個IDENTITY的值。 使用的方法是ExecuteScalar,它返回第一行的第一列(從最后一條SELECT語句返回)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.