저는 .NET에서 진짜 noob이고 SQL 서버 데이터베이스와 함께 간단한 명령 행 응용 프로그램 (C#)을 연결하려고합니다. 이제 데이터베이스에 프로그램을 연결할 수는 있지만 데이터베이스에있는 데이터는 복구 할 수 없습니다. 여기 내 코드입니다 :.NET 및 sql 서버 데이터베이스 링크
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 서버 스튜디오에서 그것을 보았다) 때, 나는 (읽기를 사용하여 데이터를 복구 할 수 없습니다) 방법. 지금까지 해본 적이 있습니다. 위조 된 것으로 datatable의 이름을 변경하십시오. datatable을 찾을 수 없습니다 (SQL Server 데이터베이스와 programm 사이의 링크가 유효 할 것 같습니다).
저는 SQL Server에서 Windows 인증을 사용하고 있습니다. 변경 사항이 생기면 몰라요. (다시 한번 : 나는 그 모든 것에 매우 익숙합니다.)
감사합니다.
테이블에 실제로 데이터가있는 경우이 코드가 작동해야합니다. 가능한 원인은 다음과 같습니다. 다른 데이터베이스를보고 있습니다. VS에서 서버 탐색기를 사용하여 코드에서 사용 된 연결 문자열과 다른 연결 문자열을 사용하는 경우 매우 일반적입니다. – Steve
그래, 전체 데이터베이스를 변경 했으므로 작동합니다. 감사 ! – Derbie
이 주석을 대답으로 변경합니다. – Steve