Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi

i want source code for insert,create,update,delete Table in DataBase workbench through .net (C#),
example
Posted
Updated 6-Sep-11 0:55am
v2
Comments
Prerak Patel 6-Sep-11 6:41am    
Elaborate.

Try these articles

ADO.Net tutorial
CP Article
 
Share this answer
 
<pre lang="c#">SqlCommand cmd = new SqlCommand("insert into technicalregistration values('" + txttechid.Text + "','" + txtname.Text + "','" + txtloginName.Text + "','" + txtpwd.Text + "','" + txtphoneno.Text + "','" + txtemail.Text + "','" + txteducation.Text + "','" + txtExperience.Text + "','" + txtaddress.Text + "','" + txtCountry.Text + "','" + txtpincode.Text + "','" + Doj + "')", (SqlConnection)Application.Get("con"));
cmd.ExecuteNonQuery();
lblmessage.Text = "Submited Successfully";</pre>


Remainning Update Delete u analize this code and write ok



Regards,

Anilkumar.D
 
Share this answer
 
v2
Comments
_Zorro_ 6-Sep-11 8:12am    
Do you know what an Sql Injection Attack is?
Anil Honey 206 6-Sep-11 8:15am    
Its A basic Code ok
_Zorro_ 6-Sep-11 8:15am    
It's not basic, it's bad.


Edit: It's bad because it's the opposite that's recommendend and because you're exposed to Sql Injection Attacks.

Edit': Take a look at this: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
Anil Honey 206 6-Sep-11 8:23am    
Hello Brother i can Post Code of 3tier and Ntier Architecture.But the Person Should learn from Bascis.What is what ok then he will inceares his coding levels.Directly if i wrire the 3tier architecture concepts he will confuse.
_Zorro_ 6-Sep-11 8:49am    
So you're saying it is easier to understand your query?
I have made an answer, just take a look at it, and tell me if you really think mine is too complicated to understand?
Hi,

You should check out some articles on Google about this, but here's a quick example:

C#
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT Table1.Field1 FROM [dbo].[Table1] WHERE Table1.Field2 = @Field2", connection); //@Field2 is a parameter

//This is where you set the @Field2 parameter value.
cmd.Parameters.Add("@Field2", SqlDbType.NVarChar).Value = your_criteria;

cmd.CommandType = CommandType.Text

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    ...
    ...
    ...
}


This way, you are Sql Injection Attacks free, and you have an 'elegant' solution.

Hope it helps...
 
Share this answer
 
Comments
Anil Honey 206 7-Sep-11 23:26pm    
I agree with u it is right.Ur sending Parameters its the good way to write.But in my coding iam not sending parameters values.Iam directly sending the values.Sql injections attacks to my code.But iam not thinking about standards.
hi prerak,
thanks to other soloutions,
i think that it will be helpful.

first of all you should do this:
C#
using system.data.sqlclient();


This code is for insert to table :
C#
private void button1_Click(object sender, EventArgs e) 
{ 
SqlConnection conection = new SqlConnection("Data Source=.;Initial Catalog=YOUR DATABASE NAME;Integrated Security=True;");
string Sql_Insert=("INSERT INTO YOUR TABLE NAME VALUES(@id, @fname, @lname)");
conection.Open(); 
try 
{ 
SqlCommand comand = new SqlCommand(Sql_Insert, conection); 
comand.Parameters.AddWithValue("@id", textBox1.Text); 
comand.Parameters.AddWithValue("@fname", textBox2.Text); 
comand.Parameters.AddWithValue("@lname", textBox3.Text); 
comand.ExecuteNonQuery();             
} 
catch(System.Data.SqlClient.SqlException ex) 
{ 
MessageBox.Show(ex.ToString()); 
} 
finally  
{ 
conection.Close(); 
} 
}


This code is for select from table :

C#
private void button1_Click(object sender, EventArgs e) 
{ 
SqlConnection conection = new SqlConnection("Data Source=.;Initial Catalog=YOUR DATABASE NAME;Integrated Security=True;");
string Sql_Select=("SELECT * FROM YOUR TABLE NAME");
conection.Open(); 
try 
{ 
SqlCommand comand = new SqlCommand(Sql_Select, conection); 
comand.ExecuteNonQuery();             
} 
catch(System.Data.SqlClient.SqlException ex) 
{ 
MessageBox.Show(ex.ToString()); 
} 
finally  
{ 
conection.Close(); 
} 
}


And etc.
I hope this is useful for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900