求和和合并
我有两个问题请教问题一
我的aspx.cs代码如下
public string ProfileNow(string getString)
{
string strConnection = "server=(local);uid=sa;pwd=sa;database=iedu";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM profile where id='" + Id + "'", objConnection);
SqlDataReader dr = cmd.ExecuteReader();
string strBody = null;
dr.Read();
strBody += "" + dr[getString] + "";
dr.Close();
objConnection.Close();
return strBody;
}
我通过上面的程序,在aspx中通过<%=ProfileNow("a1")%>和<%=ProfileNow("a2")%>分别调用出数据库中的两个值 a1和a2,我该怎么在aspx中求他们的和呢?
问题二
我在aspx.cs文件中代码都是这样的情况
public string ProfileNow1(string getString)
{
string strConnection = "server=(local);uid=sa;pwd=sa;database=iedu";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM profile where id='" + Id + "'", objConnection);
//省略
objConnection.Close();
return strBody;
}
public string ProfileNow2(string getString)
{
string strConnection = "server=(local);uid=sa;pwd=sa;database=iedu";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM profile2 where id='" + Id + "'", objConnection);
//省略
objConnection.Close();
return strBody;
}
public string ProfileNow3(string getString)
{
string strConnection = "server=(local);uid=sa;pwd=sa;database=iedu";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM profile3 where id='" + Id + "'", objConnection);
//省略
objConnection.Close();
return strBody;
}
这种情况,我想把相同的部分省略,请问改怎么做呢?
尤其是把
string strConnection = "server=(local);uid=sa;pwd=sa;database=iedu";
数据连接这块写到webconfig中,但其他代码不变,请问改怎么写呢?谢谢 --------------------编程问答-------------------- 楼主是新鸟,鉴定完毕 --------------------编程问答-------------------- 是的,是因为是菜鸟,所以请教 --------------------编程问答-------------------- web.config中添加如下代易做图
<appSettings>
<add key="dsn" value="data source=nts1;initial catalog=ehr2005_jc0303;password=1qaz;user id=sa;packet size=4096" />
</appSettings>
aspx.cs中改成如下
string strConnection=ConfigurationSettings.AppSettings("dsn")
注:我的環境是VS2003 --------------------编程问答-------------------- 那求和怎么求呢? --------------------编程问答-------------------- public int 和(int i,int j)
{
return i+j;
}
//调用
public void 方法()
{
获取a1,a2;
和(a1,a2);
int k=和;
} --------------------编程问答-------------------- 如果数据是多类型数据求和,那就用范型吧
--------------------编程问答-------------------- 同意楼上的。
补充:.NET技术 , C#