不使用存储过程 [英] without using stored procedure
本文介绍了不使用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在不使用存储过程的情况下做到这一点.我想使用数组动态创建更新字符串.我可以这样做吗?...请参见下面的代码
how can i do that without using stored procedure. i want to create update string dynamically using array. can i do that....see the below code
public int Insert(string procedureName, SortedList<string, object> parameter = null)
{
_cmd = new SqlCommand(procedureName, _con);
_cmd.CommandType = CommandType.StoredProcedure;
if (parameter != null)
{
for (int l = 0; l < parameter.Count; l++)
{
_cmd.Parameters.AddWithValue(parameter.Keys[l], parameter.Values[l]);
}
}
int _rowAffected;
try
{
if (_con.State == ConnectionState.Closed)
_con.Open();
_rowAffected = _cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (_con.State == ConnectionState.Open)
_con.Close();
}
return _rowAffected;
}
Now, Invoke this method as following...
Collapse
//first, create an object of that class... here, assume we have one object named obj
var _sl = new SortedList<string, object>();
_sl.Add("@firstName", firstName);
_sl.Add("@lastName", lastName);
_sl.Add("@age", age);
int _affectedRow = obj.Insert("InsertData",_sl);
推荐答案
这似乎非常容易,亲爱的....
It seems very Easy dear....
public int Insert(string Query, SortedList<string, object> parameter = null)
{
_cmd = new SqlCommand(Query, _con);
if (parameter != null)
{
for (int l = 0; l < parameter.Count; l++)
{
_cmd.Parameters.AddWithValue(parameter.Keys[l], parameter.Values[l]);
}
}
int _rowAffected;
try
{
if (_con.State == ConnectionState.Closed)
_con.Open();
_rowAffected = _cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (_con.State == ConnectionState.Open)
_con.Close();
}
return _rowAffected;
}
Now, Invoke this method as following...
Collapse
//first, create an object of that class... here, assume we have one object named obj
var _sl = new SortedList<string, object>();
_sl.Add("@firstName", firstName);
_sl.Add("@lastName", lastName);
_sl.Add("@age", age);
int _affectedRow = obj.Insert("INSERT INTO tablename VALUES('','','','')",_sl);
谢谢&问候,
Punit
好的,亲爱的,那么您应该传递SqlCommand对象,并且在上面的功能中,您已经像这样捕获了该对象...
Thanks & Regards,
Punit
ok dear then you should pass object of SqlCommand, and in the above fuction you have catch that object like this...
public int Insert(SqlCommand cmd)
{
cmd.Connection = con;
int _rowAffected;
try
{
if (_con.State == ConnectionState.Closed)
_con.Open();
_rowAffected = _cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (_con.State == ConnectionState.Open)
_con.Close();
}
return _rowAffected;
}
//Now, Invoke this method as following...
//first, create an object of that class. here,we assume obj is object of that class
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandText = "something"; //your procedure name or your whole Insert query like "INSERT INTO tablename VALUES(@firstName,@lastName,@age)"
cmd.AddWithValue("@firstName", firstName);
cmd.AddWithValue("@lastName", lastName);
cmd.AddWithValue("@age", age);
int _affectedRow = obj.Insert(cmd);
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for MyClass
/// </summary>
public class MyClass
{
public SqlConnection con;
public SqlCommand cmd;
public SqlDataAdapter adp;
public DataSet ds;
public MyClass()
{
//
// TODO: Add constructor logic here
//
}
public void Connection()
{
con = new SqlConnection("Connection String");
con.Open();
}
public DataSet FillDate(string MyString)
{
Connection();
adp = new SqlDataAdapter(MyString,con);
ds = new DataSet();
adp.Fill(ds);
return ds;
}
}
编写诸如filldata之类的插入函数,然后从页面中调用它.
Write insert function like filldata, and call it from pages.
这篇关于不使用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文