2017-03-03 5 views
-4

コードに間違っていることを教えてもらえますか?
データソースが良好であり、それは私に毎回同じ間違いを示している。c#sqlコマンドのプリメーラがクラッシュする

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を指定する必要があります。

関連する問題