Execute query using C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to execute some SQL code, connected directly to my database, but I don't know how to execute the query.
SqlConnection connect = new SqlConnection("DATABASE");
SqlConnection myConnection = connect;
connect.Open();
SqlDataAdapter adptador = new SqlDataAdapter(#"QUERY", connect);
DataTable tabela = new DataTable();
adptador.Fill(TABLE);
clientContext.Web.Lists.GetByTitle("TITLE");
clientContext.ExecuteQuery();
List list = clientContext.Web.Lists.GetByTitle("LISTNAME");
clientContext.Load(list);
ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
ListItem item = list.AddItem(listItemCreationInformation);
foreach (DataRow row in tabela.Rows)
{
item["LastModifiedBy"] = row["LastModifiedBy"].ToString();
item.Update();
cm = new SqlCommand("SQL COMMAND");
**EXECUTE CM**
clientContext.ExecuteQuery();
}

Depending on what your query is and what you want, you can use one of the SqlCommand Execute* methods: ExecuteNonQuery, ExecuteReader, etc.

SqlCommand has ExecuteReader, for executing the command and returning a dataset, ExecuteScalar for returning a single value of a primitive type (int, string, etc.), or executeNonQuery for returning nothing. You can also pass a command to a SqlDataAdapter, and use that to populate a DataTable object.
Please google SqlCommand and you will find LOTS of examples.

SqlDataReader reader = cm.ExecuteReader()

Related

I made an asp.net mysql query. How can I get the surname data? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I made an asp.net mysql query. How can I get the surname data?
MySqlCommand cmd = new MySqlCommand("SELECT * FROM database1 WHERE User_Name=#User_Name", conn);
cmd.Parameters.AddWithValue("#User_Name", kullanici.ToString());
reader = cmd.ExecuteReader();
sonuclbl.Text = reader["surname"].ToString();
Initially, you have to define the type of reader variable. It is not clear from the snippet you shared that you had done show.Then you have to loop through the reader, as it is described here:
var reader = cmd.Execute.Reader();
// Call Read before accessing data.
while (reader.Read())
{
// Your code here to read from the reader
}
Update
From your updated post, it is clear that you need to define the type of variable called reader.
var reader = cmd.Execute.Reader();
while (reader.Read())
{
// your code example below
string variableName = reader["ColumnName"].Tostring();
}

C# acces database select with parameter [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Hi i have problem with execute Query with Parameter in access database:
OleDbConnection cnn;
OleDbCommand cmdselect2;
string sqlselect2 = null;
string baza = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + #"L:\Windykacja\Sdro\Projekt\projekt.accdb";
connetionString = baza;
sqlselect2 = "SELECT count(POS_Pesel_regon) as Suma FROM POS WHERE POS_Pesel_regon = #PR";
cnn = new OleDbConnection(connetionString);
cnn.Open();
cmdselect2 = new OleDbCommand(sqlselect2, cnn);
Int32 PR1 = Convert.ToInt32(cmdselect2.ExecuteScalar());
cmdselect2.Parameters.AddWithValue("#PR", textBox6.Text);
cmdselect2.Dispose();
cnn.Close();
It's say that my paramter is missing
In insert it works perfectly :)
will be thankfull for any sugestions.
cheers
Because you try to execute your command before you add your parameter. Change those lines
Int32 PR1 = Convert.ToInt32(cmdselect2.ExecuteScalar());
cmdselect2.Parameters.AddWithValue("#PR", textBox6.Text);
to
cmdselect2.Parameters.AddWithValue("#PR", textBox6.Text);
Int32 PR1 = Convert.ToInt32(cmdselect2.ExecuteScalar());
A few things more;
Use using statement to dispose your connection and command automatically instead of calling Close or Dispose methods manually.
Don't use AddWithValue as much as you can. It may generate unexpected and surprising results sometimes. Use Add method overload to specify your parameter type and it's size.
using(var cnn = new OleDbConnection(connetionString))
using(var cmdselect2 = cnn.CreateCommand())
{
cmdselect2.CommandText = #"SELECT count(POS_Pesel_regon) as Suma FROM POS
WHERE POS_Pesel_regon = #PR";
cmdselect2.Parameters.Add("#PR", OleDbType.VarChar).Value = textBox6.Text;
// I assumed your column type as VarChar
cnn.Open();
int PR1 = (int)cmdselect2.ExecuteScalar();
}

IF ELSE condition asp net with SQL Server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
string conn = "";
conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();
SqlConnection objsqlconn = new SqlConnection(conn);
objsqlconn.Open();
SqlCommand objcmd = new SqlCommand("IF (select 1 from PRODUCT where PRODUCT_NAME=" + Master_product_txt.Text + ")=1
PRINT 'ALREADY AVAILABLE'
ELSE
Insert into PRODUCT(PRODUCT_NAME) Values('" + Master_product_txt.Text + "')
GO", objsqlconn);
objcmd.ExecuteNonQuery();
MessageBox.Show("Details Successfully Added!!!");
I'm trying check the data base values before insert the value, I've wrote query for it, it's working in sql server environment, I could not able to implement same thing in Visual Studio
go is a SSMS (SQL Server Management Studio) statement, it won't work from C#
use parameters to avoid SQL injection
it is unusual to use the Hungarian obj prefix in C#
A quick try at a better version:
var cmd = new SqlCommand(#"
IF NOT EXISTS (SELECT * FROM PRODUCT WHERE PRODUCT_NAME = #NAME)
BEGIN
INSERT INTO PRODUCT (PRODUCT_NAME) VALUES (#NAME)
END
", sqlconn);
cmd.Parameters.AddWithValue("#NAME", Master_product_txt.Text);
cmd.ExecuteNonQuery();
SqlCommand objcmd = new SqlCommand("SELECT 1 from PRODUCT WHERE PRODUCT_NAME=#NAME" , objsqlconn);
//NVarChar
cmd.Parameters.Add("#NAME", SqlDbType.NVarChar,20).Value = Master_product_txt.Text;
objsqlconn.Open();
readr = SelectCommand.ExecuteReader();
if (!readr.HasRows)
{
`// code to insert values here.
}`
PRINT 'ALREADY AVAILABLE' will not work here.For capturing print statement message you have to add an event handler to the InfoMessage event on the connection.And use parametrized query where ever possible. ;)

insert multiple records asp.net sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
there is a table to store borrowed items called borrowTable, lets say a customer take multiple items, I know how to select and insert a single item but how to select and insert multiple items in a single click?
Take all your data in a datatable(We can call 'myTable' here) and you can insert as follows
string sql = "INSERT INTO MyTable (Col1, Col2) VALUES (#Value1, #Value2)";
using (MySqlConnection con = new MySqlConnection(connectionString))
{
int retvalue;
con.Open();
foreach (DataRow row in myTable.Rows)
{
MySqlCommand myCommand = new MySqlCommand();
myCommand.Connection = con;
myCommand.CommandText = sql;
myCommand.Parameters.AddWithValue("#Value1", r["Value1"]);
myCommand.Parameters.AddWithValue("#Value2", r["Value2"]);
retvalue = myCommand.ExecuteNonQuery();
}
}
The simplest way to copy lots of data from any resources to SQL Server is BulkCopying.
See this example.

How to query from table's view in .Net C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to query on table's view in .Net C# web application ?
for example, here [View_App_Academic] is my table view. My code is listed below. under db scheme, I am not able to see the view due to my user privilege.
string strquery = "select * from [dbo].[View_App_Academic] where recruitment_id=" +
RecruitDropDownList.Text + " and ref_no='" + RefDropDownList.Text + "'";
SqlCommand objCMD = new SqlCommand(strquery, conn);
Use parameterized query always.
Remove [dbo] from your query, you don't need to add [dbo] because it is default database schema.
Change your code to this.
string strquery = "select * from View_App_Academic where recruitment_id=#recruitment_id and ref_no=#ref_no";
SqlCommand objCMD = new SqlCommand(strquery, conn);
objCMD.Parameters.AddWithValue("#recruitment_id", RecruitDropDownList.Text);
objCMD.Parameters.AddWithValue("#ref_no",RefDropDownList.Text);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = objCMD;
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
Hope it helps.

Categories

Resources