友人遇到问题,数据库中无存储过程,而想写带参数的插入语句。
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 中的部分代码,虽然我的是 数据库中已经建立了存储过程了。不过,个人认为,无论如何其基本原理是一样的,形式不同而已。今天帖我的代码于此,以便遇到此类问题,了做参考。
/***删除文件***/#region/***删除文件***/
public int DeleteFileInfo(string name)
{
int rowCount=-1;
try
{
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)
{
throw new Exception(ex.Message);
}
finally
{
if (conn.State!=ConnectionState.Closed)
conn.Close();
}
return rowCount;
}
#endregion
/***查询类型***/#region/***查询类型***/
/**//// <summary>
/// 查询现有关键字段数据
/// </summary>
/// <param name="tablename">表名</param>
/// <param name="field">字段名</param>
/// <param name="name">DataSet填充表名</param>
/// <returns>DataSet </returns>
public DataSet ComboxFieldData()
{
SqlDataAdapter da;
string selectSQL=string.Empty;
selectSQL="select distinct 1 , 2 from ##";
conn=ConnectToSqlSever();
da = new SqlDataAdapter(selectSQL,conn);
DataSet ds=new DataSet();
try
{
da.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn.State!=ConnectionState.Closed)
conn.Close();
}
return ds;
}
#endregion
/**//***************************************************************************/
/***object转***/#region/***object转***/
private FileInfomation ChangeObj(object [] Row)
{
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];
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;
}
private FileInfomation ChangeObj2(object [] Row)
{
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];
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);
FileInfomation tempExam=new FileInfomation(sn,Name.ToString(),as.ToString(),
.ToString(),terial,te.ToString(),x.ToString(),
fid,me.ToString(),lfgth,shid);
return tempExam;
}
#endregion
/***插入数据***/#region/***插入数据***/
public void InsertFileInfomation(object [] obj)
{
FileInfomation fileinfo=ChangeObj(obj);
SqlParameter[] sqlpara=new SqlParameter[10];
sqlpara[0]=new SqlParameter("@Name",fileinfo.me );
sqlpara[1]=new SqlParameter("@Alias",fileinfo.ile);
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);
sqlpara[7]=new SqlParameter("@me ",fileinfo.M);
sqlpara[8]=new SqlParameter("@ength",fileinfo.ength);
sqlpara[9]=new SqlParameter("@ag",fileinfo.ag);
SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["sqlConnString"]);
Database db=new Database(conn);
try
{
db.Excute("UP##",sqlpara);
}
catch(Exception e)
{
throw e;
}
finally
{
db.Close();
}
}
#endregion
/***更新数据***/#region/***更新数据***/
public void UpdateFileInfomation(object [] obj)
{
FileInfomation fileinfo=ChangeObj2(obj);
SqlParameter[] sqlpara=new SqlParameter[11];
sqlpara[0]=new SqlParameter("@Name",fileinfo.me);
sqlpara[1]=new SqlParameter("@Alias",fileinfo.ile);
sqlpara[2]=new SqlParameter("@UserID",fileinfo.erid);
sqlpara[3]=new SqlParameter("@Type", fileinfo.Type);
sqlpara[4]=new SqlParameter("@Date",fileinfo.ate);
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
{
db.Excute("U##",sqlpara);
}
catch(Exception e)
{
throw e;
}
finally
{
db.Close();
}
}
#endregion