我是新手C#程序員。我試圖用C#做遊戲。在我的註冊頁面有一些錯誤。它說ExecuteNonQuery:Connection屬性還沒有初始化在這行「int result = command.ExecuteNonQuery();」。這是我的C#代碼。請幫我解決這個問題。如何解決ExecuteNonQuery:連接屬性尚未初始化
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 WiimoteTest
{
public partial class regispage : Form
{
private SqlConnection connection;
private string sql;
private SqlCommand command = new SqlCommand();
public regispage()
{
InitializeComponent();
}
private void back_Click(object sender, EventArgs e)
{
startpage back = new startpage();
back.Show();
this.Close();
}
private void regispage_Load(object sender, EventArgs e)
{
string conStr = @"Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Users\vaio\Downloads\Compressed\WiimotLib_1.7\WiimotLib_1.7\samples\WiimoteTestCS_5_AllpagePuppyGameTestBirdResizepicTestDB\Angee.mdf;
Intregrated Security=True;User Instance=True;";
connection = new SqlConnection(conStr);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
}
private bool IsCompleteData()
{
string errMsg = "";
if (username.Text == "")
{
errMsg = "Please input username";
}
else if (password.Text == "")
{
errMsg = "Please input password";
}
else if (fname.Text == "")
{
errMsg = "Please input firstname";
}
else if (lname.Text == "")
{
errMsg = "Please input lastname";
}
else if (nickname.Text == "")
{
errMsg = "Please input nickname";
}
else if (gender.SelectedIndex == -1)
{
errMsg = "Please select gender";
}
else if (age.Text == "")
{
errMsg = "Please input age";
}
else if (symptom.Text == "")
{
errMsg = "Please input symptom";
}
if (errMsg == "")
{
return true;
}
else
{
MessageBox.Show(errMsg);
return false;
}
}
private void AddParamterValues()
{
command.Parameters.AddWithValue("username", username.Text);
command.Parameters.AddWithValue("password", password.Text);
command.Parameters.AddWithValue("fname", fname.Text);
command.Parameters.AddWithValue("lname", lname.Text);
command.Parameters.AddWithValue("nickname", nickname.Text);
command.Parameters.AddWithValue("gender", gender.SelectedItem.ToString());
command.Parameters.AddWithValue("age", age.Text);
command.Parameters.AddWithValue("symptom", symptom.Text);
}
private void submit_Click(object sender, EventArgs e)
{
if (!IsCompleteData())
{
return;
}
InsertData();
}
private void InsertData()
{
using (var connection = new SqlConnection())
using (var command = connection.CreateCommand())
{
sql = @"INSERT INTO User(username, password, fname, lname, nickname, gender, age, symptom)
VALUES(@username, @password, @fname, @lname, @nickname, @gender, @age, @symptom),this.connection";
**connection.Open();**
var affectedRows = command.ExecuteNonQuery();
command.Parameters.Clear();
command.CommandText = sql;
AddParamterValues();
if (affectedRows < 1)
{
MessageBox.Show("Error to insert data");
return;
}
else
{
MessageBox.Show("Insert data complete");
menupage submit = new menupage();
submit.Show();
this.Close();
}
}
}
private void regispage_FormClosing(object sender, FormClosingEventArgs e)
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
}
Thx幫助。在那一行,我已經刪除了「//」評論,但它仍然有相同的錯誤。我應該怎麼做:( – user2187208 2013-03-19 16:06:57
command.Connection =連接 – 2013-03-19 16:09:50
我已經修改我的代碼爲command.Connection = connection;但它仍然不起作用 – user2187208 2013-03-19 16:14:04