我的查询命令未将值插入表中。
我已仔细检查以确保我已正确连接到SQL Server。运行代码时没有任何错误,但是当我执行nonquery函数时,它始终返回0,这意味着我的查询不成功。我查看了所有资源。我的语法似乎正确,但是我不确定。
Form1.cs(我的应用程序的添加数据按钮的用户界面)
public partial class Storage : Form
{
storageMLGW s = new storageMLGW();
public Storage()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
s.Manufacturer = textBoxManufacturer.Text;
s.Model = textBoxModel.Text;
s.SerialNo = textBoxSerialNo.Text;
s.Item_Type = textBoxItemType.Text;
s.Building = textBoxBuilding.Text;
s.RoomNo = textBoxRoomNo.Text;
s.FaxNo = textBoxFaxNo.Text;
s.IP_Address = textBoxIPAddress.Text;
s.UserAssigned = textBoxUserAssigned.Text;
s.AreaName = textBoxAreaName.Text;
s.AreaNo = textBoxAreaNo.Text;
bool isSuccess = s.Insert(s);
if (isSuccess == true)
{
MessageBox.Show("New Product successfully inserted.");
}
else
{
MessageBox.Show("Failed to add new product. Try again.");
}
}
}
storageMLGW.cs
class storage MLGW
{
//Getters and setters
public int ProductID { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public string SerialNo { get; set; }
public string Item_Type { get; set; }
public string Building { get; set; }
public string RoomNo { get; set; }
public string FaxNo { get; set; }
public string IP_Address { get; set; }
public string UserAssigned { get; set; }
public string AreaName { get; set; }
public string AreaNo { get; set; }
static string myconnstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
public bool Insert(storageMLGW s)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstring);
try
{
string sql = "INSERT INTO storage_mlgw (Manufacturer, Model, SerialNo, Item_Type, Building, RoomNo, FaxNo, IP_Address, UserAssigned, AreaName, AreaNo) " +
"VALUES(@Manufacturer, @Model, @SerialNo, @Item_Type, @Building, @RoomNo, @FaxNo, @IP_Address, @UserAssigned, @AreaName, @AreaNo)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Manufacturer", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@Model", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@SerialNo", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@Item_Type", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@Building", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@RoomNo", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@FaxNo", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@IP_Address", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@UserAssigned", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@AreaName", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@AreaNo", SqlDbType.VarChar, 50);
cmd.Parameters["@Manufacturer"].Value = s.Manufacturer;
cmd.Parameters["@Model"].Value = s.Model;
cmd.Parameters["@SerialNo"].Value = s.SerialNo;
cmd.Parameters["@Item_Type"].Value = s.Item_Type;
cmd.Parameters["@Building"].Value = s.Building;
cmd.Parameters["@RoomNo"].Value = s.RoomNo;
cmd.Parameters["@FaxNo"].Value = s.FaxNo;
cmd.Parameters["@IP_Address"].Value = s.IP_Address;
cmd.Parameters["@UserAssigned"].Value = s.UserAssigned;
cmd.Parameters["@AreaName"].Value = s.AreaName;
cmd.Parameters["@AreaNo"].Value = s.AreaNo;
conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
catch(Exception ex)
{
}
finally
{
conn.Close();
}
return isSuccess;
}
}
我希望输出返回true并显示消息框“产品已成功添加”。而是,Insert方法返回false。