2017-03-03 4 views
-4

코드에 어떤 문제가 있는지 말해 줄 수 있습니까?
데이터 소스는 양호하고, 그 같은 실수를 저마다 도시되어C# sql command premeter crashing

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data.SqlClient; 

namespace DatabaseActions 
{ 
    class Program 
    { 
     public static void AddUser(string firstname, string lastname, string username, string password, string password2, string email, string location, string birthday, string gender) 
     { 
      string connectionstring = @"Data Source=(localdb)\MSSQLLocalDB;initial catalog=GedgetsDB"; 

      SqlConnection connection = new SqlConnection(connectionstring); 

      connection.Open(); 
      string insert1 = "INSERT INTO TBL_Users(FirstName, LastName, UserName, Password, Password2, Email, Location, Birthday, Gender)"; 
      string insert2 = "VALUES ('" + firstname + "' , '" + lastname + "' , '" + username + "' , '" + password + "' , '" + password2 + "' , '" + email + "' , '" + location + "' , '" + birthday + "' , '" + gender+"')"; 
      string Insertcommandtext = insert1 + insert2; 

      SqlCommand command = new SqlCommand(Insertcommandtext, connection); 
      command.ExecuteNonQuery(); 
      connection.Close(); 
     } 
     static void Main(string[] args) 
     { 
      Console.Write("Enter firstname: "); 
      string firstname = Console.ReadLine(); 
      Console.Write("Enter lastname: "); 
      string lastname = Console.ReadLine(); 
      Console.Write("Enter username: "); 
      string username = Console.ReadLine(); 
      Console.Write("Enter password: "); 
      string password = Console.ReadLine(); 
      Console.Write("Enter password2: "); 
      string password2 = Console.ReadLine(); 
      Console.Write("Enter email: "); 
      string email = Console.ReadLine(); 
      Console.Write("Enter location: "); 
      string location = Console.ReadLine(); 
      Console.Write("Enter birthday: "); 
      string birthday = Console.ReadLine(); 
      Console.Write("Enter gender: "); 
      string gender = Console.ReadLine(); 
      AddUser(firstname, lastname, username, password, password2, email, location, birthday, gender); 

     } 
    } 
} 

'System.Data.SqlClient.SqlException' 유형의 처리되지 않은 예외가 system.data.dll

발생을

추가 정보 : 로그인이 에 의해 요청 된 데이터베이스 "GedgetsDB"를 열 수 없습니다. 로그인에 실패했습니다.

'AMITPC \ עמית'사용자가 로그인하지 못했습니다.

누구나 수정 방법을 알고 있습니까?

+2

다른 리소스에 대한 링크가 없으므로 다음 번에 코드를 게시하시기 바랍니다. – Reborn

+1

Windows 통합 인증을 사용하고 있습니까? – Gnqz

답변

0

데이터베이스를 살펴보고 로그인 'AMITPC \ עמית'이 열려고하는 데이터베이스에 액세스 할 수 있는지 확인해야합니다. 로그인에 액세스 할 수있는 경우 응용 프로그램이 구성의 올바른 데이터베이스로 연결되는지 확인하십시오.

0

SQL 서버를 사용하는 경우 실제로 언급 된 사용자의 로그인을 만들거나 User IDpwd을 지정하기 위해 연결 문자열을 편집해야합니다.