private void button1_Click(object sender, System.EventArgs e) {
SqlConnection conn = new SqlConnection("Data Source=localhost;Database=Test;Integrated Security=SSPI");
//string jn="SELECT Student_info.FName,Login.User1 FROM Student_info INNER JOIN Login ON Student_info.FName=Login.User1";
SqlCommand command = new SqlCommand("t", conn);
// t: Stored procedure. command.CommandType=CommandType.StoredProcedure;
// command.Parameters.Add("@User1", SqlDbType.VarChar).Value =textBox1.Text;
command.CommandText="t";
conn.Open();
DataSet ds=new DataSet();
DataTable myTable=ds.Tables.Add("Login");
SqlDataAdapter dt=new SqlDataAdapter(command.CommandText,conn); dt.Fill(ds,"login");
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string s=reader[0].ToString().Trim()+""+reader[1].ToString().Trim();
MessageBox.Show(s);
}
conn.Close();
}
}
For Answers, see/post comments
How to call a stored procedure from C#
Subscribe to:
Post Comments (Atom)
2 comments:
u declare objects for
sql connnection cn;
sql parameter p;
then in the button code
button()
{
try
{
dmd=new sqlcommand("insrec",cn);
cmd.commandtype=commandtype.storedprocedure;
p=new sqlparameter("@eid",sqldbtype.int);
p.value=txtid.tostring();
cmd.parameters.add(p);
----like that u write code for ename,and other fields also
then write
cmd.executeNonquery();
}
catch(exception ex)
{
}
public int Insert(Person person)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("InsertData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@firstName", person.FirstName);
dCmd.Parameters.AddWithValue("@lastName", person.LastName);
dCmd.Parameters.AddWithValue("@age", person.Age);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
Above code is a code snippet copied from 4-Tier architecture article at http://www.dotnetfunda.com/articles/article18.aspx
Post a Comment