我使用visual studio 2010開發了一個asp.net web應用程序。我使用SQL Server 2008作爲數據庫。我需要通過LAN連接從遠程服務器獲取數據,並且必須通過C#代碼存儲到我的數據庫。我是初學者,使用asp.net和C#進行web開發。請告訴我一個非常簡單的方法來解決這個問題。從遠程SQL服務器獲取數據
這是我試過的一個例子。這段代碼是否正確或錯誤,請指導我這個?
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = @"Data Source=172.16.7.127\inhouse;User ID=****;Password= *****;";
con1.Open();
string str = "select * from t_his_events Where patientMRN="+hosp_no.Text;
SqlDataReader dr;
SqlCommand myCommand = new SqlCommand(str,con1);
dr = myCommand.ExecuteReader();
if (dr.HasRows)
{
Console.WriteLine("connection established");
}
}
歡迎的StackOverflow,一個Q&A爲開發人員支持社區DataSet對象。請詳細說明您可能嘗試過的內容,包括現有的代碼,並概述您遇到的具體問題。我們不會爲您編寫代碼。請點擊編輯按鈕以包含此信息。 – SBirthare