i want to use insert query by using SQL server 2005. I MAKE TABLE in sql server nad give them name of column but this is not giving proper output..
plz check what is the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace insert_method
{
class Program
{
static void Main(string[] args)
{
string constring ="Server=LAB02-19\SQLEXPRESS;Database=insert";
SqlConnection conn = new SqlConnection();
SqlCommand cmd;
conn.Open();
string Sql="Insert into info values" (stname , rollnum);
insert table_1(ali','1');
int a= cmd.ExecuteNonQuery();
}
}
}
Loading
Wil YoumansPosted Apr 16, 2011, 7:37 PM
// Write parameters into your sql with a @
string Sql="Insert into info (stname, rollnuminsert) values (@stname , @rollnuminsert)";
// Put the connection string in the SqlConnection
SqlConnection conn = new SqlConnection(constring);
// Give the command the connection and sql statement
SqlCommand cmd = new SqlCommand(Sql, conn);
// Get your data
string stname = TextBox1.Text; // just example
int rollnuminsert = Int32.Parse(TextBox2.Text); // just example
// You have to give the command parameters that are in your sql statement
cmd.Parameters.Add("@stname", SqlDbType.VarChar).Value = stname;
cmd.Parameters.Add("@rollnuminsert", SqlDbType.Int).Value = rollnuminsert;
// Make a data adapter
SqlDataAdapter daMain = new SqlDataAdapter();
daMain.InsertCommand = cmd;
// Open the connection
conn.Open();
// Execute the command
int a = daMain.InsertCommand.ExecuteNonQuery();
// Close the connection
conn.Close();
------------------------
Here is another example.
SqlConnection sc = new SqlConnection(Properties.Settings.Default.PSRConnectionString);
String strSql = "Insert Into clientTimes (ClientID, TimeIn, TimeOut) " +
"VALUES(@ClientID,@Time1, @Time2)";
SqlCommand cmdInsert = new SqlCommand(strSql, sc);
cmdInsert.Parameters.Add("@ClientID", SqlDbType.Int).Value = clid;
cmdInsert.Parameters.Add("@Time1", SqlDbType.DateTime).Value = time1;
cmdInsert.Parameters.Add("@Time2", SqlDbType.DateTime).Value = time2;
daMain.InsertCommand = cmdInsert;
sc.Open();
daMain.InsertCommand.ExecuteNonQuery();
sc.Close();
I'm new to this to but the above seem like the standard steps(if not using TableAdapters)
Hope this helps.
matt cuprykPosted Apr 8, 2011, 1:38 PM
tAhIra sgdPosted Apr 4, 2011, 11:18 PM
Krishna GaradPosted Mar 28, 2011, 3:40 AM
string Sql="Insert Into Info(Stname,rollnum)values('ali',1);