我正在为大学的项目讲座开发酒店管理系统。我请你帮我修改下面粘贴的代码。我不明白为什么它不传输和保存我在Visual Studio Express中输入的程序输入。这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
class Database
{
SqlConnection con;
public Database()
{
con = new SqlConnection("server=.,database=Guest; Integrated Security = True");
}
public bool AddGuest(string ID, string Phone, string Adress, string FName, string LName, string Email, string RoomNo)
{
bool durum = false;
SqlCommand cmd = new SqlCommand(@"declare @guest_id nvarchar(50),@guest_phone nvarchar(50),@guest_adress ntext,@guest_fname ntext,@guest_lname ntext,@guest_email ntext,@room_no int = @room_no;
select @guest_id = Guest.guest_id from Guest where Guest.room_no = '103';
select @guest_phone = Guest.guest_phone from Guest where Guest.room_no = '103';
select @guest_adress = Guest.guest_adress from Guest where Guest.room_no = '103';
select @guest_fname = Guest.guest_fname from Guest where Guest.room_no = '103';
select @guest_lname = Guest.guest_lname from Guest where Guest.room_no = '103';
select @guest_email = Guest.guest_email from Guest where Guest.room_no = '103';
insert into Guest(guest_id,guest_phone,guest_adress,guest_fname,guest_lname,guest_email,room_no)values(@guest_id,@guest_adress,@guest_fname,@guest_lname,@guest_email,@room_no)
)", con);
cmd.Parameters.AddWithValue("@id", ID);
cmd.Parameters.AddWithValue("@phone", Phone);
cmd.Parameters.AddWithValue("@adress", Adress);
cmd.Parameters.AddWithValue("@fname", FName);
cmd.Parameters.AddWithValue("@lname", LName);
cmd.Parameters.AddWithValue("@email", Email);
cmd.Parameters.AddWithValue("@room_no", RoomNo);
con.Open();
try
{
cmd.ExecuteNonQuery();
durum = true;
}
catch (Exception)
{
}
con.Close();
return durum;
}
private void ekle_Click(object sender, EventArgs e)
{
}
}
}
例如,正如您在图片中看到的那样,我正在输入访客ID,名字,姓氏,然后我希望将它们保存在数据库中。我将数据库与visual连接起来,并根据它创建了代码。但仍然行不通。
答案 0 :(得分:0)
删除该SQL语句并将其放入SQL Server管理工作室并查看它是否运行。你会看到它会抛出一些错误,主要是因为你的变量。
我在简要回顾中发现的一些事情: