Trying to insert data into SQL Server 
Author Message
 Trying to insert data into SQL Server

Hi,

I'm learning ASP.NET and C# and for some reason I couldn't find a single
tutorial that covers this. All I'm trying to do is insert some data into a
SQL Server database. All the examples that I've come across use the DataSet.
Isn't unnecessary to use it if all I'm doing is just insert data? Could
someone point me in the right direction for a sample or a tutorial? Thanks.

Sam



Mon, 06 Dec 2004 23:11:34 GMT  
 Trying to insert data into SQL Server


Quote:
> Hi,

> I'm learning ASP.NET and C# and for some reason I couldn't find a single
> tutorial that covers this. All I'm trying to do is insert some data into a
> SQL Server database. All the examples that I've come across use the
DataSet.
> Isn't unnecessary to use it if all I'm doing is just insert data? Could
> someone point me in the right direction for a sample or a tutorial?

Thanks.

You can create a SqlCommand object and set it's command string to a standard
SQL INSERT statement then use ExecuteNonQuery() to fire it.

Alternatively, you can create a stored procedure on the SQL server and
use the SqlCommand to call it as well (set CommandType to StoredProcedure
and pass in a bunch of SqlParameters to the Parameters property).

Please see the System.Data.SqlClient namespace in the .NET Framework
SDK docs.

Also, you can use DataSets and such if you wish, but if you just want
simple DB access, it can be overkill.

-c



Mon, 06 Dec 2004 23:19:28 GMT  
 Trying to insert data into SQL Server
Sam... This code demonstrates common requirements: a transaction,
executing a nonquery, returning the autonumber primary key and closing
the connection. Indenting removed.

string myConnString= "Persist Security Info=False;User ID=sa;Initial
Catalog=pubs;Data Source=(local);";
System.Data.SqlClient.SqlConnection myConnection = new
System.Data.SqlClient.SqlConnection(myConnString);
System.Data.SqlClient.SqlTransaction myTrans;
System.Data.SqlClient.SqlCommand myCommand = new
System.Data.SqlClient.SqlCommand();
try
{
myConnection.Open();    
try
{
// Start a local transaction
myTrans = myConnection.BeginTransaction();
try
{
// Must assign both transaction object and connection
// to Command object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;
.. // more statements here
myCommand.CommandText = "Insert into Test (Name) VALUES ('New')";
myCommand.ExecuteNonQuery();

//MessageBox.Show(myCommand.ExecuteScalar().ToString());
myTrans.Commit();
//MessageBox.Show("All records are written to database.");
catch(Exception e)
myTrans.Rollback();
MessageBox.Show(e.Message+"Neither record was written to database.");
finally
{
myConnection.Close();

Quote:
}
}

catch(Exception e1)
{
//MessageBox.Show(e1.Message);
myConnection.Close();
Quote:
}
}

catch (Exception e)
{
//MessageBox.Show(e.Message);

Quote:
}

Jeff
Quote:
>All I'm trying to do is insert some data into a

SQL Server database<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Tue, 07 Dec 2004 11:09:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Inserting Binary data into SQL Server

2. SQL Server: Slow INSERT performance??

3. Inserting a Delimited Text File into SQL Server

4. Insert in SQL Server CTable<CDynamicAccessor>

5. OLEDB, Fail Insert, SQL Server 7

6. How to insert an image into SQL Server?

7. Inserting Muliple Records into SQL-SERVER Database

8. Inserting a Delimited Text File into SQL Server

9. Inserting a Delimited Text File into SQL Server

10. Inserting in to a SQL Server Database

11. connect to sql server and insert

12. How to insert image into SQL Server?

 

 
Powered by phpBB® Forum Software