我无法从c#app将数据保存到我的sql数据库,甚至没有给我任何错误。我错过了什么。它是一个简单的脚本,它将从Textbox中获取用户输入,并将复选框插入SQL数据库。 这是我的剧本。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Kaizen_Tracking_System_V1
{
public partial class Individual : Form
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Asus\Documents\Visual Studio 2010\Projects\Kaizen Tracking System V1\Kaizen Tracking System V1\Database1.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
public Individual()
{
InitializeComponent();
}
private void Individual_Load(object sender, EventArgs e)
{
cmd.Connection = cn;
}
private void button1_Click(object sender, EventArgs e)
{
if (logTxtBox.Text != "" & lnameTextBox.Text != "" & fnameTextBox.Text != "" & depCheckBox1.Text != "" & DepCheckBox2.Text != "" & depCheckBox3.Text != "" & depCheckBox4.Text != "" & locationComboBox1.Text != "" & processTextBox.Text != "" & typeTextBox.Text != "" & odgrecdataTextBox.Text != "" & kimpdateTextBox.Text != "" & cipaTextBox.Text != "" & cspmTextBox.Text != "" & rewardgivenTextBox.Text != "" & rcppTextBox.Text != "" & kvdTextBox.Text != "" & ylocationTextBox.Text != "" & detailRichTextBox1.Text != "")
{
cn.Open();
cmd.CommandText = "insert into kaizentracker (lognum,lname,fname,dept,location,process,type,odgrecdate,kimpdate,cipa,cspm,rewardgiven,rcpp,kverifieddate,ylocation,details) values ('" + logTxtBox.Text + "' , '" + lnameTextBox.Text + "' , '" + fnameTextBox.Text + "' , '" + depCheckBox1.Text + "' , '" + DepCheckBox2.Text + "' , '" + depCheckBox3.Text + "' ,'" + depCheckBox4.Text + "' , '" + locationComboBox1.Text + "' , '" + processTextBox.Text + "' , '" + typeTextBox.Text + "' , '" + odgrecdataTextBox.Text + "' , '" + kimpdateTextBox.Text + "' , '" + cipaTextBox.Text + "' , '" + cspmTextBox.Text + "' , '" + rewardgivenTextBox.Text + "' , '" + rcppTextBox.Text + "' , '" + kvdTextBox.Text + "' , '" + ylocationTextBox.Text + "' , '" + detailRichTextBox1.Text + "') ";
cmd.ExecuteNonQuery();
cmd.Clone();
MessageBox.Show("Data Saved");
cn.Close();
logTxtBox.Text = "";
lnameTextBox.Text = "";
fnameTextBox.Text = "";
depCheckBox1.Text = "";
DepCheckBox2.Text = "";
depCheckBox3.Text = "";
depCheckBox4.Text = "";
locationComboBox1.Text = "";
processTextBox.Text = "";
typeTextBox.Text = "";
odgrecdataTextBox.Text = "";
kimpdateTextBox.Text = "";
cipaTextBox.Text = "";
cspmTextBox.Text = "";
rewardgivenTextBox.Text = "";
rcppTextBox.Text = "";
kvdTextBox.Text = "";
ylocationTextBox.Text = "";
detailRichTextBox1.Text = "";
}
}
}
}
答案 0 :(得分:7)
Initial catalog
。你应该在那里提到你的数据库名称。
@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Asus\Documents\Visual Studio 2010\Projects\Kaizen Tracking System V1\Kaizen Tracking System V1\Database1.mdf;Initial Catalog=MyDatabase; Integrated Security=True;User Instance=True"
答案 1 :(得分:3)
1.使用double&&符号而不是单一和在if条件块中
替换为:
if (logTxtBox.Text != "" & lnameTextBox.Text != "" & fnameTextBox.Text != "" & depCheckBox1.Text != "" & DepCheckBox2.Text != "" & depCheckBox3.Text != "" & depCheckBox4.Text != "" & locationComboBox1.Text != "" & processTextBox.Text != "" & typeTextBox.Text != "" & odgrecdataTextBox.Text != "" & kimpdateTextBox.Text != "" & cipaTextBox.Text != "" & cspmTextBox.Text != "" & rewardgivenTextBox.Text != "" & rcppTextBox.Text != "" & kvdTextBox.Text != "" & ylocationTextBox.Text != "" & detailRichTextBox1.Text != "")
以下内容:
if (logTxtBox.Text != "" && lnameTextBox.Text != "" && fnameTextBox.Text != "" && depCheckBox1.Text != "" && DepCheckBox2.Text != "" && depCheckBox3.Text != "" && depCheckBox4.Text != "" && locationComboBox1.Text != "" && processTextBox.Text != "" && typeTextBox.Text != "" && odgrecdataTextBox.Text != "" && kimpdateTextBox.Text != "" && cipaTextBox.Text != "" && cspmTextBox.Text != "" && rewardgivenTextBox.Text != "" && rcppTextBox.Text != "" && kvdTextBox.Text != "" && ylocationTextBox.Text != "" && detailRichTextBox1.Text != "")
2.您尝试在表格中插入的值多于查询中指定的值。
您已指定要插入表中的16个值,如下所示:
"insert into kaizentracker(lognum,lname,fname,dept,location,process,type,odgrecdate,kimpdate,cipa,cspm,rewardgiven,rcpp,kverifieddate,ylocation,details)"
但您要插入19个值,如下所示:
values ('" + logTxtBox.Text + "' , '" + lnameTextBox.Text + "' , '" + fnameTextBox.Text + "' , '" + depCheckBox1.Text + "' , '" + DepCheckBox2.Text + "' , '" + depCheckBox3.Text + "' ,'" + depCheckBox4.Text + "' , '" + locationComboBox1.Text + "' , '" + processTextBox.Text + "' , '" + typeTextBox.Text + "' , '" + odgrecdataTextBox.Text + "' , '" + kimpdateTextBox.Text + "' , '" + cipaTextBox.Text + "' , '" + cspmTextBox.Text + "' , '" + rewardgivenTextBox.Text + "' , '" + rcppTextBox.Text + "' , '" + kvdTextBox.Text + "' , '" + ylocationTextBox.Text + "' , '" + detailRichTextBox1.Text + "') ";
3.您在SQL连接字符串中缺少数据库名称。
替换为:
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Asus\Documents\Visual Studio 2010\Projects\Kaizen Tracking System V1\Kaizen Tracking System V1\Database1.mdf;Integrated Security=True;User Instance=True");
有以下内容: 例如,您的数据库名称= samplesatabase
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Asus\Documents\Visual Studio 2010\Projects\Kaizen Tracking System V1\Kaizen Tracking System V1\Database1.mdf;Initial Catalog=sampledatabase;Integrated Security=True;User Instance=True");
4.使用参数化查询以避免SQL注入攻击:
示例:
string SqlCommand= "INSERT INTO myTable ([param1],[param2])VALUES(@param1,@param2)";
command.Parameters.Add("@param1", SqlDbType.NVarChar,50);
command.Parameters.Add("@param2", SqlDbType.NVarChar,50);
command.Parameters["@param1"].Value = name1;
command.Parameters["@param2"].Value = name2;
5.将您的代码包装到try-catch / finally块中:
示例:
try {
//DB Statements
}
finally
{
//handle exceptions and close all open connections
}
6.在操作结束时关闭您的Sql连接对象。
示例:
try
{
SqlConnection connection = new SqlConnection(strConnectionString);
connection.Open();
}
finally
{
connection.Close();
}
答案 2 :(得分:0)
整个用户实例和AttachDbFileName = 方法存在缺陷 - 充其量!在Visual Studio中运行应用程序时,它将复制.mdf
文件(从App_Data
目录到输出目录 - 通常是.\bin\debug
- 应用程序运行的地方)和最有可能,您的INSERT
工作正常 - 但您最后只是查看错误的.mdf文件!
如果你想坚持这种方法,那么尝试在myConnection.Close()
调用上设置一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf
文件 - 我几乎可以肯定你的数据在那里。
我认为真正的解决方案将是
安装SQL Server Express(你已经完成了)
安装SQL Server Management Studio Express
在 SSMS Express 中创建数据库,为其指定一个逻辑名称(例如KaizenDatabase
)
使用其逻辑数据库名称(在服务器上创建时给定)连接到它 - 并且不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:
Data Source=.\\SQLEXPRESS;Database=KaizenDatabase;Integrated Security=True
其他所有内容都完全与以前相同......