[英].NET and sql server database link
我是.NET中的真正菜鳥,而且我正嘗試將一個簡單的命令行應用程序(在C#中)與SQL Server數據庫鏈接。 現在,我可以將程序與數據庫連接,但無法恢復其中的數據。 這是我的代碼:
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
string queryString = "SELECT USER_ID FROM dbo.ISALLOCATEDTO;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
int i = 0;
while (reader.Read())
{
i++;
Console.WriteLine("Field "+i);
Console.WriteLine("\t{0}",reader[0]);
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
//Console.WriteLine("Hello world");
string x = Console.ReadLine();
}
static private string GetConnectionString()
{
return "Data Source=FR401388\\SQLEXPRESS;Initial Catalog=Test;";
+ "Integrated Security=SSPI";
}
}
}
但是,當我運行它時,即使我的表不為空(我在sql server studio中也看到過),我也無法通過使用read()方法恢復數據。 到目前為止,我所做的是:嘗試使用偽造的數據表更改數據表的名稱:找不到數據表(因此sql server數據庫和programm之間的鏈接似乎有效)。
我正在sql服務器中使用Windows身份驗證,如果正在更改任何內容,則不知道...(再次:我對所有這些都很陌生)。
謝謝 !
您的代碼應該可以工作。
可能的原因是:您正在查看其他數據庫。
如果在VS中使用Server Explorer且連接字符串與代碼中使用的連接字符串不同,則這是很常見的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.