const string PARM_PRODUCTSTYPE_ID = "@ID";
const string Path=@"c:\a.xml";
const string SQL_UPDATE_PRODUCTSTYPEINFO = "UPDATE ProductsType SET TypeName = ?,[Level] = ? WHERE [ID]= ?;";以上的“@”、“?”的用法是什么啊?
const string Path=@"c:\a.xml";
const string SQL_UPDATE_PRODUCTSTYPEINFO = "UPDATE ProductsType SET TypeName = ?,[Level] = ? WHERE [ID]= ?;";以上的“@”、“?”的用法是什么啊?
const string Path=@"c:\a.xml"; 路径用@const string SQL_UPDATE_PRODUCTSTYPEINFO = "UPDATE ProductsType SET TypeName = ?,[Level] = ? WHERE [ID]= ?;"; ?三元运算符
http://xxx.xxx.com/pageID=1就可以定义一个常量为
const string strPageFormat="http://xxx.xxx.com/pageID=1";到时候循环替换如下:for(int i=1;i<=10;i++)
{
string strHtml=GetPageContent(strPageFormat.Replace("ID=1","ID="+i));
}
http://xxx.xxx.com/pageID=1就可以定义一个常量为
const string strPageFormat="http://xxx.xxx.com/pageID=|pageID|";到时候循环替换如下:for(int i=1;i <=10;i++)
{
string strHtml=GetPageContent(strPageFormat.Replace("|pageID|",i));
}
第二个表示@后面的是字符串.忽略转义字符.
第三个表示参数.用参数化的方式执行sql用到.
const string Path=@"c:\a.xml"; 转义用的把\\转义成\
const string SQL_UPDATE_PRODUCTSTYPEINFO = "UPDATE ProductsType SET TypeName = ?,[Level] = ? WHERE [ID]= ?;"; 应该是调用access的
就是转换OleDBParameter参数用的
比如
OleDBConnection conn=new OleDBConnection("");
OleDBCommand cmd=new OleDBCommand(SQL_UPDATE_PRODUCTSTYPEINFO,conn);
cmd.Parameter.Add(new OleDbParameter("?",DataType.Varchar));
大概这个意思。懒的打了;网上好多这样的资料。
如果 CommandType 设置为 Text,OLE DB .NET 提供程序不支持将参数传递给 OleDbCommand 调用的 SQL 语句或存储过程的命名参数。在这种情况下,必须使用问号 (?) 占位符。例如: SELECT * FROM Customers WHERE CustomerID = ? 因此,OleDbParameter 对象添加到 OleDbParameterCollection 的顺序必须直接对应于命令文本中参数的问号占位符的位置。下面的示例创建一个 OleDbCommand 并显示其参数。要完成此任务,请向该方法传递一个 OleDbConnection、一个查询字符串(它是 SQL SELECT 语句)和一个 OleDbParameter 对象数组。public void CreateMyOleDbCommand(OleDbConnection connection,
string queryString, OleDbParameter[] parameters)
{
OleDbCommand command = new OleDbCommand(queryString, connection);
command.CommandText =
"SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?";
command.Parameters.Add(parameters); for (int j=0; j<parameters.Length; j++)
{
command.Parameters.Add(parameters[j]) ;
} string message = "";
for (int i = 0; i < command.Parameters.Count; i++)
{
message += command.Parameters[i].ToString() + "\n";
}
Console.WriteLine(message);
}
private const string SQL_INSERT_PRODUCTSTYPEINFO = "INSERT INTO ProductsType ( [TypeName], [Level] ) VALUES ( ?,? );";
//ms-sql里面用参数对象,字符中用命名的参数做为占位符。这里的命名的参数必须与参数对象定义(getParameterSql)的名字一致
private const string SQL_INSERT_PRODUCTSTYPEINFO_sql = "INSERT INTO ProductsType ( [TypeName], [Level] ) VALUES ( @ProductsTypeName,@ProductsTypeLevel );";
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
insertSQL();
//insertAccess();
} //得到参数对象Access
public OleDbParameter[] getParameterAccess(string paramName, int paramLevel)
{
OleDbParameter[] param = new OleDbParameter[]
{
new OleDbParameter("ProductsTypeName",OleDbType.Char),
new OleDbParameter("ProductsTypeLevel",OleDbType.Integer)
};
param[0].Value = paramName;
param[1].Value = paramLevel;
return param;
} //得到参数对象SQL
public SqlParameter[] getParameterSql(string paramName, int paramLevel)
{
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@ProductsTypeName",SqlDbType.Char),
new SqlParameter("@ProductsTypeLevel",SqlDbType.Int),
};
param[0].Value = paramName;
param[1].Value = paramLevel;
return param;
}
private static string GetAccessPath()
{
string strConn;
BasePage BP = BasePage.Instance();
string filePath = BP.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings.Get("AccessPath"));
strConn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + filePath;
return strConn;
} //访问Access数据库
public void insertAccess()
{
//OleDbConnection con = new OleDbConnection(GetAccessPath());
OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =D:\\My Documents\\Visual Studio 2005\\WebSites\\WebSiteht\\App_Data\\dbht.mdb");
OleDbCommand com = new OleDbCommand(SQL_INSERT_PRODUCTSTYPEINFO, con);
OleDbParameter[] paramcom = this.getParameterAccess("张正明", 110);
for (int i = 0; i < paramcom.Length; i++)
{
com.Parameters.Add(paramcom[i]);
}
try
{
con.Open();
com.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{ ex.ToString(); }
} //访问SQL数据库
public void insertSQL()
{
SqlConnection con = new SqlConnection("server=(local);database=GM_B225;uid=sa;pwd=sa");
SqlCommand com = new SqlCommand(SQL_INSERT_PRODUCTSTYPEINFO_sql, con); SqlParameter[] paramcom = getParameterSql("张正明", 110);
for (int i = 0; i < paramcom.Length; i++)
{
com.Parameters.Add(paramcom[i]);
}
try
{
con.Open();
com.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{ ex.ToString(); }
}