Delete data from sql server(C#)
-
hi friends How i can delete data from data base(sql server) in C#?????
-
hi friends How i can delete data from data base(sql server) in C#?????
this is simple script to delete using webform asp.net C# here is my code: 1. adding connection in Web.config Web.config ==========
<connectionStrings>
<add name="myConnection" providerName="System.Data.SqlClient" connectionString="server=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True"/>
</connectionStrings>2. in default.aspx default.aspx ============ added namespace: using System.Data.SqlClient; using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string connProvider = ConfigurationManager.ConnectionStrings["myConnection"].ToString();
using (SqlConnection cnn = new SqlConnection(connProvider))
{
cnn.Open();
string userid = "1";
string query = "Delete Users Where UserID = '" + userid + "'";
using (SqlCommand cmd = new SqlCommand(query, cnn))
{
cmd.ExecuteNonQuery();
}
}
}
}hope this help. (Update)= this code not recommended for develop, you can try it for learn and you can improve your code. regard, Ade Ruyani
-
this is simple script to delete using webform asp.net C# here is my code: 1. adding connection in Web.config Web.config ==========
<connectionStrings>
<add name="myConnection" providerName="System.Data.SqlClient" connectionString="server=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True"/>
</connectionStrings>2. in default.aspx default.aspx ============ added namespace: using System.Data.SqlClient; using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string connProvider = ConfigurationManager.ConnectionStrings["myConnection"].ToString();
using (SqlConnection cnn = new SqlConnection(connProvider))
{
cnn.Open();
string userid = "1";
string query = "Delete Users Where UserID = '" + userid + "'";
using (SqlCommand cmd = new SqlCommand(query, cnn))
{
cmd.ExecuteNonQuery();
}
}
}
}hope this help. (Update)= this code not recommended for develop, you can try it for learn and you can improve your code. regard, Ade Ruyani
Reason for my downvote: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Even in a simple example like this, it takes hardly any extra work to do it properly!
Never underestimate the power of stupid things in large numbers --- Serious Sam
-
Reason for my downvote: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Even in a simple example like this, it takes hardly any extra work to do it properly!
Never underestimate the power of stupid things in large numbers --- Serious Sam
Thank you sam, this is just a simple code, whether they can improve they're code using store procedure or else.
-
hi friends How i can delete data from data base(sql server) in C#?????
int RowsAffected = 0;
using(SqlConnection cn = new SqlConnection(ConnectionStringHere))
{
cn.Open();using(SqlCommand cmd = new SqlCommand())
{
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;//This shows how to delete with a simple where clause cmd.CommandText = "Delete from tablename where id = @id"; cmd.Parameters.AddWithValue("@id",id); RowsAffected = cmd.ExecuteNonQuery();
}
}Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON
-
Thank you sam, this is just a simple code, whether they can improve they're code using store procedure or else.
-
hi friends How i can delete data from data base(sql server) in C#?????
You can use ado.net: A Beginner's Tutorial for Understanding ADO.NET[^] Or any ORM like entity framework for the same: An Introduction to Entity Framework for Absolute Beginners[^]
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.