PDA

View Full Version : سوال: مشکل این کد چیه؟



ehsan_2000
یک شنبه 29 آذر 1388, 15:41 عصر
سلام
من میخوام یه برنامه با # c بنویسم که اطلاعات textbox رو تو sql ذخیره کنه . ولی هیچ کاری انجام نمی ده .

اینم کداش

namespace WindowsApplication2

{

class Class1
{
public string id,name,family,codemelli,date,phone,address;

public bool D_Insert()
{
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "Data Source=s1master\\sqlexpress; " +
" database=Information;integrated security=true";
SqlCommand MyCommand = new SqlCommand();
MyCommand.Connection = MyConnection;
MyCommand.CommandType = System.Data.CommandType.Text;
MyCommand.CommandText = "Insert Into person (id,name,family,codemelli,datebirth,mobile,address ) " +
" Values (" + id + ",'" + name + "'," + family + ",'"+codemelli + ",'" + date + ",'" + phone + ",'" + address + ")";
MyConnection.Open();
if (MyCommand.ExecuteNonQuery() > 0)
return true;
else
return false;


}


}
}


private void button1_Click(object sender, EventArgs e)
{
Class1 obj = new Class1();
obj.id=textBox1.Text;
obj.name=textBox2.Text;
obj.family=textBox3.Text;
obj.codemelli=textBox4.Text;
obj.date=textBox5.Text;
obj.phone=textBox6.Text;
obj.address=textBox7.Text;
obj.D_Insert;
}

slashslash2009
یک شنبه 29 آذر 1388, 15:57 عصر
+ "'," + family + ",'"+codemelli + ",'" + date + ",'" + phone + ",'" + address +

بعضی جاهارو یادت رفته ' بزاری

میتونستی از class هم استفاده نکنی راحت تر بود کارت

ehsan_2000
یک شنبه 29 آذر 1388, 16:04 عصر
میتونستی از class هم استفاده نکنی راحت تر بود کارت

میشه بیشتر توضیح بدین؟

slashslash2009
یک شنبه 29 آذر 1388, 18:26 عصر
میتونی کدهای insert رو در یک button بزاری اینجوری راحت تری آخه اینجا نیازی به کلاس نیست و مقدارهاتو(value) رو مستقیم از درون تکست باکسات بگیری مثل :



con2.doCommand("insert into Table1(name,code)values('" + textbox1.Text + "','" + textbox2.tex + "')");

البته کد بالا فقط یک مثاله ولی واضحه

ehsan_2000
یک شنبه 29 آذر 1388, 21:36 عصر
من این کد رو که می نویسم این خطا رو می ده :
خطا :

The name "میرزائی" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
این کد :

class D_Kala
{
public string Codep, Namep, familyp,code,date,ph,add;

public bool D_Insert()
{
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = ("server=localhost; database=Information;integrated security=true");
SqlCommand MyCommand = new SqlCommand();
MyCommand.Connection = MyConnection;
MyCommand.CommandType = System.Data.CommandType.Text;
MyCommand.CommandText = "Insert Into dbo.Person_Info (id,name,family,codemelli,datebirth,phone,address) " +
" Values (" + Codep + ",'" + Namep + "'," + familyp + "','"+code+"','"+date+"','" + ph+ "','" + add + ")";
MyConnection.Open();
if (MyCommand.ExecuteNonQuery() > 0)
return true;
else
return false;


}


}

Parham.D
یک شنبه 29 آذر 1388, 21:46 عصر
کدهای زیر صحیح است و کار میکند. با کمی تغییر میشود آن چه که شما میخواهید.






<!-- /* Font Definitions */ @font-face {font-family:&quot;Cambria Math&quot;; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:&quot;&quot;; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:&quot;Times New Roman&quot;,&quot;serif&quot;; mso-fareast-font-family:&quot;Times New Roman&quot;; mso-bidi-language:AR-SA;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> public partial class _Default : System.Web.UI.Page
{
//Declaration SqlConnection, SqlDataReader and StringCommand.
string StringCommand = "INSERT INTO employee " + "(ID, Fname, Lname, Age )" + "VALUES (@ID, @Fname, @Lname, @Age)";
SqlConnection connect = new SqlConnection("Data Source='(local)\\SQLExpress';Integrated Security=True;Pooling=False;Initial Catalog='LaboratoryDB'");

protected void btnInsert_Click(object sender, EventArgs e)
{
SqlCommand Command = new SqlCommand(StringCommand, connect);

Command.CommandType = CommandType.Text;

Command.Parameters.AddWithValue("@ID", int.Parse(tbID.Text));
Command.Parameters.AddWithValue("@Fname", tbFname.Text);
Command.Parameters.AddWithValue("@Lname", tbLname.Text);
Command.Parameters.AddWithValue("@Age", tbAge.Text);

connect.Open();
Command.ExecuteNonQuery();
connect.Close();
}

slashslash2009
یک شنبه 29 آذر 1388, 22:18 عصر
," + familyp + "'

یک کتیشن کم گذاشتی
" ' " + family+" ' "

ehsan_2000
دوشنبه 30 آذر 1388, 10:36 صبح
ک کتیشن کم گذاشتی

درست کردم حالا این خطا رو میده :


Unclosed quotation mark after the character string 'kl;)'.
Incorrect syntax near 'kl;)'.

ehsan_2000
دوشنبه 30 آذر 1388, 11:55 صبح
ببخشید یه سوال دیگه .
اگه من این برنامه رو روی کامژیوتر دیگه اجرا کنم sql کار می کنه . یعنی مسیرش و ژیدا می کنه ؟
من شنیدم باید sa تعریف بشه ولی نمی دونم چجوری ؟