PDA

View Full Version : سوال: پرکردن فیلد های sql با دستور insert to



darbdar
سه شنبه 12 فروردین 1393, 12:26 عصر
با سلام.من یک فرم ساده با یک textbox و button طراحی کردم که هرچی اسم داخل textboxنوشته می شه با دکمه در تیبلی که براش طراحی ردم بشینه.کد نوشته شده در button : public partial class Form1 : Form {
SqlConnection test = new SqlConnection(" server=127.0.0.1;Initial Catalog=majid;Integrated Security=True");
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
test.Open();
}


private void button1_Click(object sender, EventArgs e)
{

SqlCommand test1 = new SqlCommand("insert into personal (Name)VALUES ('"+textBox1.Text+"''",test);
test1.ExecuteNonQuery();
test.Close();
}
حالا وقتی به خط execute میرسه این پیامو میده:
Unclosed quotation mark after the character string 'ali''.
Incorrect syntax near 'ali''.
کسی می تونه کمکم کنه؟؟؟؟

hamid_hr
سه شنبه 12 فروردین 1393, 12:30 عصر
textBox1.Text+"''",test
اینجا چرا دو تا ' گذاشتی

darbdar
سه شنبه 12 فروردین 1393, 12:44 عصر
'" برای تموم شدن معرفی تکست باکس و یه دونه هم برای معرفی sqlconnection که بالا براش تعریف کردم

Yanehsar
سه شنبه 12 فروردین 1393, 13:10 عصر
برادر من راست میگه دو تا احتیاج نیست باید یه دونه بذاری

darbdar
سه شنبه 12 فروردین 1393, 13:30 عصر
syntax error میده

Yanehsar
سه شنبه 12 فروردین 1393, 13:38 عصر
به جا این کدها از اینها استفاده کنید ببینید چطوری میشه بازم پیام میده یا نه .


try
{
if (con.State != ConnectionState.Closed)
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into TableName(Name)values(@Name)";
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
cmd.ExecuteNonQuery();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State != ConnectionState.Open)
con.Close();
}

darbdar
سه شنبه 12 فروردین 1393, 15:00 عصر
دوست عزیز یه سوال دیگه من کدی را که داده بودید به حالت زیر تغییر دادم .


try
{
if (test.State != ConnectionState.Closed)
test.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = test;
cmd.CommandText = "insert into Table(Name,family,jobs,zipcod)values(@Name,@family ,@zipcod,@jobs)";
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text;
cmd.Parameters.Add("@family", SqlDbType.NVarChar).Value = textBox2.Text;
cmd.Parameters.Add("@jobs", SqlDbType.NVarChar).Value = textBox3.Text;
cmd.Parameters.Add("@zipcod", SqlDbType.Int).Value = Int32.Parse(textBox4.Text.Trim());
cmd.ExecuteNonQuery();


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (test.State != ConnectionState.Open)
test.Close();




}
}
}
الان این پیامو میده
the connection was not closed .the connections current state is open
چیزی هم وارد دیتا بیس نمی شه

Yanehsar
سه شنبه 12 فروردین 1393, 17:05 عصر
معذرت می خوام دو تا if برعکس نوشتم اولی باید Open باشه و پائینی باید close باشد به این صورت

try
{
if (con.State != ConnectionState.Open)
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State != ConnectionState.Closed)
con.Close();
}