PDA

View Full Version : مشکلinsert



User-os
یک شنبه 08 اردیبهشت 1387, 16:46 عصر
سلام .من معذرت میخوام چون هر چی سرچیدم نیافتم.دوستان من تازه دارم سی شارپ رو می اموزم.یه برنامه ساده نوشتم که سه تا دکمه داره insert,delete,loginدستور select شو راه انداختم منتها تو دستور insert مشکل دارم این کد select که خوب جواب داد:


()cnn.Open
cmd=new OleDbCommand();
cmd.CommandText=" select * from table1 where first='"+tb1.Text+"'";
cmd.Connection = cnn;
da=new OleDbDataAdapter();
da.SelectCommand=cmd;
dset=new DataSet();
da.Fill(dset);
if (dset.Tables[0].Rows.Count>0 )
MessageBox.Show("true");

cnn.Close();
اینجا یکم بهم میریزه ببخشید کد insert رو اگه کامل بدید ممنون

captain_black81
یک شنبه 08 اردیبهشت 1387, 16:57 عصر
insert into table1 (name,id) values ('ali','20')

captain_black81
یک شنبه 08 اردیبهشت 1387, 17:04 عصر
دوست من ، بهتره برای insert یک stored procedure بسازی و اونو بصورت یک کلاس در #C استفاده کنی

gdevnb
یک شنبه 08 اردیبهشت 1387, 17:26 عصر
cmd = new SqlCommand("Inser Into Table1 Values(1,'X')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

علیرضا مداح
یک شنبه 08 اردیبهشت 1387, 17:29 عصر
سلام ،
مثالی از MSDN :


public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
adapter.InsertCommand = command;
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
return adapter;
}