2017-03-03 31 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

请考虑下次发布代码,因为它没有链接到SO以外的其他资源。 – Reborn

+1

我想你正在使用Windows集成身份验证? – Gnqz

回答

0

您应该查看数据库并确保您的登录'AMITPC \עמית'有权访问您尝试打开的数据库。 如果登录有权访问,请检查您的应用程序是否指向配置中的正确数据库。

0

如果您使用的是SQL Server,则需要为所提及的用户实际创建一个登录名,或编辑您的连接字符串以指定User IDpwd