友人遇到问题,数据库中无存储过程,而想写带参数的插入语句。
sql里面+了"@"表示sql里面的参数,而你开始定义的只是一个字符串.刚才那个没有赋值,正确看下面的.
SqlConnection conn=new SqlConnection(".....");
string name1 = "", age1 = "";
string strSqlInsert = "insert into birthday1(name1,age1) values (@name1,@age1)";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strSqlInsert;
cmd.Parameters.Add("@name1", System.Data.SqlDbType.VarChar, 10).Value = name1; ;
cmd.Parameters.Add("@age1", System.Data.SqlDbType.VarChar, 10).Value=age1;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
昨天我最想找FileOperWebServices下
public class GetWebData 中的部分代码,虽然我的是 数据库中已经建立了存储过程了。不过,个人认为,无论如何其基本原理是一样的,形式不同而已。今天帖我的代码于此,以便遇到此类问题,了做参考。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***删除文件***/#region/***删除文件***/
public int DeleteFileInfo(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int rowCount=-1;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringBuilder strDelete =new StringBuilder();
strDelete.Append(" delete from iles");
strDelete.Append(" where Name='");
strDelete.Append(name);
strDelete.Append("'");
conn=ConnectToSqlSever();
SqlCommand deleteCmd = new SqlCommand(strDelete.ToString(),conn);
rowCount=deleteCmd.ExecuteNonQuery();
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(ex.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (conn.State!=ConnectionState.Closed)
conn.Close();
}
return rowCount;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***查询类型***/#region/***查询类型***/
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 查询现有关键字段数据
/// </summary>
/// <param name="tablename">表名</param>
/// <param name="field">字段名</param>
/// <param name="name">DataSet填充表名</param>
/// <returns>DataSet </returns>
public DataSet ComboxFieldData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlDataAdapter da;
string selectSQL=string.Empty;
selectSQL="select distinct 1 , 2 from ##";
conn=ConnectToSqlSever();
da = new SqlDataAdapter(selectSQL,conn);
DataSet ds=new DataSet();
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
da.Fill(ds);
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(ex.Message);
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (conn.State!=ConnectionState.Closed)
conn.Close();
}
return ds;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//***************************************************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***object转***/#region/***object转***/
private FileInfomation ChangeObj(object [] Row)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object Name= Row[0];
object alias=Row[1];
object serId= Row[2];
object Se= Row[3];
object Date= Row[4];
object x= Row[5];
object F= Row[6];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
object me=Row[7];
object egth=Row[8];
object ag=Row[9];
int ty=System.Convert.ToInt32(ial);
int d=System.Convert.ToInt32(F);
int shid=System.Convert.ToInt32(sag);
long lfgth=System.Convert.ToInt64(filgth);
FileInfomation tempExam=new FileInfomation(Name.ToString(),as.ToString(),
uId.ToString(),terial,uploadDate.ToString(),x.ToString(),
flaid,me.ToString(), lfength,sgid);
return tempExam;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private FileInfomation ChangeObj2(object [] Row)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object uestion=Row[0];
object Name= Row[1];
object s=Row[2];
object upe= Row[3];
object al= Row[4];
object Date= Row[5];
object fx= Row[6];
object fF= Row[7];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
object me=Row[8];
object fiegth=Row[9];
object slag=Row[10];
int sn=System.Convert.ToInt32(Ion);
int tyrial=System.Convert.ToInt32(Sial);
int id=System.Convert.ToInt32(fF);
int sgid=System.Convert.ToInt32(shfla);
long lngth=System.Convert.ToInt64(fiegth);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FileInfomation tempExam=new FileInfomation(sn,Name.ToString(),as.ToString(),
.ToString(),terial,te.ToString(),x.ToString(),
fid,me.ToString(),lfgth,shid);
return tempExam;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***插入数据***/#region/***插入数据***/
public void InsertFileInfomation(object [] obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfomation fileinfo=ChangeObj(obj);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlParameter[] sqlpara=new SqlParameter[10];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlpara[0]=new SqlParameter("@Name",fileinfo.me );
sqlpara[1]=new SqlParameter("@Alias",fileinfo.ile);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlpara[2]=new SqlParameter("@ID",fileinfo.erid);
sqlpara[3]=new SqlParameter("@rial", fileinfo.rial);
sqlpara[4]=new SqlParameter("@Date",fileinfo.ate);
sqlpara[5]=new SqlParameter("@x",fileinfo.x);
sqlpara[6]=new SqlParameter("@F",fileinfo.F);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlpara[7]=new SqlParameter("@me ",fileinfo.M);
sqlpara[8]=new SqlParameter("@ength",fileinfo.ength);
sqlpara[9]=new SqlParameter("@ag",fileinfo.ag);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["sqlConnString"]);
Database db=new Database(conn);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.Excute("UP##",sqlpara);
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.Close();
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***更新数据***/#region/***更新数据***/
public void UpdateFileInfomation(object [] obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfomation fileinfo=ChangeObj2(obj);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlParameter[] sqlpara=new SqlParameter[11];
sqlpara[0]=new SqlParameter("@Name",fileinfo.me);
sqlpara[1]=new SqlParameter("@Alias",fileinfo.ile);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlpara[2]=new SqlParameter("@UserID",fileinfo.erid);
sqlpara[3]=new SqlParameter("@Type", fileinfo.Type);
sqlpara[4]=new SqlParameter("@Date",fileinfo.ate);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlpara[5]=new SqlParameter("@File",fileinfo.x);
sqlpara[6]=new SqlParameter("@F",fileinfo.F);
sqlpara[7]=new SqlParameter("@me ",fileinfo.Me);
sqlpara[8]=new SqlParameter("@ength",fileinfo.ength);
sqlpara[9]=new SqlParameter("@lag",fileinfo.lag);
sqlpara[10]=new SqlParameter("@ID",fileinfo.ID);
SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["sqlConnString"]);
Database db=new Database(conn);
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.Excute("U##",sqlpara);
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw e;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
db.Close();
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)