0

我正在尝试通过我的 C# 代码连接到我的数据库(在同一台计算机上)。问题是我不断收到Login failed for user " "错误...

这是我的代码的一部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hotel_Essencia
{
public partial class CheckIn : Form
{
    public CheckIn()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Database= HotelManagement; server= Sherissa\SQLEXPRESS";
        con.Open();

        SqlCommand cmd = new SqlCommand("insert into CheckIn (@TransactionId,@GuestName,@RoomType,@RoomNo,@ReservationDate,@CheckInDate,@CheckOutDate,@NoOfDays,@NoOfAdults,@NoOfChildren),", con);
        cmd.Parameters.AddWithValue("@TransactionId",textBox1.Text);
        cmd.Parameters.AddWithValue("@GuestName", textBox2.Text);
        cmd.Parameters.AddWithValue("@RoomType", textBox3.Text);
        cmd.Parameters.AddWithValue("@RoomNo", textBox4.Text);
        cmd.Parameters.AddWithValue("@ReservationDate", textBox5.Text);
        cmd.Parameters.AddWithValue("@CheckInDate", textBox6.Text);
        cmd.Parameters.AddWithValue("@CheckOutDate", textBox7.Text);
        cmd.Parameters.AddWithValue("@NoOfDays", textBox8.Text);
        cmd.Parameters.AddWithValue("@NoOfAdults", textBox9.Text);
        cmd.Parameters.AddWithValue("@NoOfChildren", textBox10.Text);


        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("DATA ADDED SUCCESSFULLY!!");
    }

    private void CheckIn_Load(object sender, EventArgs e)
    {

    }
}
}
4

1 回答 1

4

您需要在连接字符串中安排凭据,如果您没有凭据并且在本地工作,那么您可以使用集成安全性逃脱:

Integrated Security=SSPI; Trusted_Connection=True;

当您为数据库显式设置用户时,请相应地添加凭据:

User Id=username;Password=password;
于 2013-05-30T09:05:56.317 回答