我有一个问题,我找不到解决方案,即在 Visual Studio 中将数据插入 LocalDBWindows Form App - C#
实际上,我使用的是类库,并在我的 Windows 窗体应用程序中添加了对它的引用问题是什么时候我想向本地数据库添加新数据,它没有显示任何错误,但是,我没有看到数据插入到数据库中
这是我在类库中的代码,这是一种通用方法,可用于将任何数据添加到数据库中的任何表中
public virtual void Add(Item item)
{
int count = 0;
//create the first part of the Add SQL string
string addString = "INSERT INTO " + table + "(";
//add each field name from Item UpdateFields to the string
foreach (KeyValuePair<string, string> field in item.UpdateFields)
{
addString += "[" + field.Key + "]";
count++;
//add a comma when end of UpdateFields collection is reached
if (count < item.UpdateFields.Count)
{ addString += ", "; }
}
//start second part of Add string
addString += ") VALUES(";
count = 0;
//add each value from Item UpdateFields to the string
foreach (KeyValuePair<string, string> field in item.UpdateFields)
{
if (field.Value != null)
{ addString += "'" + field.Value.ToString() + "'"; }
else
{ addString += "NULL"; }
count++;
//add a comma when end of UpdateFields collection is reached
if (count < item.UpdateFields.Count)
{ addString += ", "; }
}
//add bracket at end of Add string
addString += ")";
command.CommandText = addString;
try
{
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
item.Valid = false;
item.ErrorMessage = ex.Message;
}
}
在这里我用它来添加帐户信息
cc = new Account(accountID);
acc.UserName = userNametxt.Text;
acc.Password = "P@ssword";
acc.Email = emailtxt.Text;
acc.CreatedBy = "";
acc.Status = "1";
acc.LastLoginDateTime = null;
acc.LastFailureLoginDateTime = null;
accounts.Add(acc);
if (!acc.Valid)
{
MessageBox.Show(acc.ErrorMessage);
}
else
{
MessageBox.Show("Account has been added successfully!!");
}
以下是我的字符串连接
new SqlConnection(Properties.Settings.Default.TestConnectionString);
提前致谢