无法将数据插入数据库! [英] Cannot insert data into database!
问题描述
这些是我的源代码。我使用Visual Studio 2010和.NET Framework 4.0版
我无法将数据插入数据库。
>>图片<<
< img border =0src =http://image.free.in.th/v/2013/tt/626x1.jpgalt =free.in.th的图片/> < br $> b $ b
< img border =0 src =http://image.free.in.th/v/2013/tp/o5fm2.jpgalt =free.in.th的图片/>
< img border =0src =http:/ /image.free.in.th/v/2013/tm/ns8q3.jpgalt =图片由free.in.th/>
--------------------------------------- -------------------------------------------------- ------
>>源代码<<
These are my source code. I use Visual Studio 2010 and .NET Framework Version 4.0
I can not insert data into database.
>> Picture <<
<img border="0" src="http://image.free.in.th/v/2013/tt/626x1.jpg" alt="images by free.in.th"/>
<img border="0" src="http://image.free.in.th/v/2013/tp/o5fm2.jpg" alt="images by free.in.th"/>
<img border="0" src="http://image.free.in.th/v/2013/tm/ns8q3.jpg" alt="images by free.in.th" />
-----------------------------------------------------------------------------------------------
>> Source code <<
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 DataGridTest_WithNoDatabase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection connection;
private SqlCommand command;
private DataSet dataSt;
private SqlDataAdapter adapter;
private string sql;
private void Form1_Load(object sender, EventArgs e)
{
//Connect Database Part
/////////////////////////////////////////////////////////////////
string conStr = @"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Order.mdf;
Integrated Security=True; User Instance=True";
connection = new SqlConnection(conStr);
connection.Open();
/////////////////////////////////////////////////////////////////
sql = "SELECT * FROM Order_Table";
command = new SqlCommand(sql, connection);
adapter = new SqlDataAdapter(command);
dataSt = new DataSet();
adapter.Fill(dataSt, "Order_Table");
dataGridView1.DataSource = dataSt.Tables["Order_Table"];
string[] headers={"orderID", "Name", "Made In"};
for (int i = 0; i < headers.Length; i++)
{
dataGridView1.Columns[i].HeaderText = headers[i];
}
dataGridView1.Rows[0].Height = 22;
}
private void InsertData()
{
sql = @"INSERT INTO Order_Table(orderID, Name, madeIn)
VALUES(@txtOrderID, @txtName, @txtMadeIn)";
command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("txtOrderID", Convert.ToInt32(txtOrderID.Text));
command.Parameters.AddWithValue("txtName", txtName.Text);
command.Parameters.AddWithValue("txtMadeIn", txtMadeIn.Text);
int result = (int)command.ExecuteNonQuery();
if (result != -1)
{
MessageBox.Show("Add Data Completed");
}
else
{
MessageBox.Show("Error ! !");
}
}
private void button1_Click(object sender, EventArgs e)
{
InsertData();
connection.Close();
}
}
}
推荐答案
已在InsertData()函数中进行了更改。试试这个。还添加了一些特殊的处理和连接管理。
have made changes in the InsertData() function. Try this. Also add some exceptional handling and connection management.
private void InsertData()
{
sql = "INSERT INTO Order_Table(orderID, Name, madeIn)
VALUES(@txtOrderID, @txtName, @txtMadeIn)";
string conStr = "Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Order.mdf;
Integrated Security=True; User Instance=True";
connection = new SqlConnection(conStr);
connection.Open();
command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("txtOrderID", Convert.ToInt32(txtOrderID.Text));
command.Parameters.AddWithValue("txtName", txtName.Text);
command.Parameters.AddWithValue("txtMadeIn", txtMadeIn.Text);
int result = (int)command.ExecuteNonQuery();
if (result != -1)
{
MessageBox.Show("Add Data Completed");
}
else
{
MessageBox.Show("Error ! !&");
}
connection.Close();
}
这是我的项目。
在此下载>>
http://www.boyr.com/getfile.php?id=1395506&key=51dd0b5db1a17
This is my project.
Download here >>
http://www.boyr.com/getfile.php?id=1395506&key=51dd0b5db1a17
这篇关于无法将数据插入数据库!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!