我使用visual studio 2010开发了一个asp.net Web应用程序。我使用SQL Server 2008作为数据库。我需要通过LAN连接从远程服务器之一获取数据,并且必须通过C#代码存储到我的数据库。我是使用asp.net和C#进行Web开发的初学者。请告诉我一个解决这个问题的简单方法。
这是我试过的一个例子。这段代码是好还是错,请指导我这个?
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");
}
}
答案 0 :(得分:0)
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection(<connectionstring>);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM table";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
现在您有一个包含查询结果的DataSet。至于确定你的,我建议Connection Strings作为选择完美格式的绝佳资源。
答案 1 :(得分:0)
您在远程服务器上创建的connectionString中的数据库名称在哪里
使用System.Data.SqlClient; 使用system.Data;
string cs = Data Source=172.16.7.127\inhouse;database=databasename;User ID=User id create on remote server;Password= Your password for the ;
SqlConnection conn = new SqlConnection(cs);
SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM tableName",conn);
DataSet ds = new DataSet();
adp.Fill(ds);
现在您可以使用包含表格的数据集对象