如何在.aspx 里连接SQL Server??

alderchew 2004-04-13 10:48:03
菜鸟问题,在.aspx 里加上什么脚本, 可以连接SQL Server 里的Table , 做个样本,可以吗??
...全文
350 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenen 2004-04-14
  • 打赏
  • 举报
回复
建议你看一下书,先打点基础
smx717616 2004-04-14
  • 打赏
  • 举报
回复
oooo
来晚了
说一下两种连接方式吧
1。Connection-DataAdapter-DataSet-DataGrid
2. Connection-Command-DataReader
第二种速度快,不占用内存,
第一中用于显示多数据时,很方便。方法看上面的就行了
wggwan 2004-04-14
  • 打赏
  • 举报
回复
首先在web.cofig中配置连接数据库
<appSettings>
<add key="ds" value="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Hospital;connect timeout=120" />
</appSettings>
在.aspx.cs中连接数据库

using System.Data.SqlClient;
using System.Configuration;
protected System.Data.SqlClient.SqlCommand cm;
protected System.Data.SqlClient.SqlConnection cn;
#region 向数据库中插入新记录 InsertNewUser()
private void InsertNewUser()
{
cn = new SqlConnection(ConfigurationSettings.AppSettings["ds"]);//连接数据库操作。
string strInsert="insert into user_info(user_id,username,pwd,user_truename,Mobile,Address) values('"+UserID.Text+"','"+UserName.Text+"','"+Pwd.Text+"','"+TrueName.Text+"','"+Mobile.Text+"','"+Address.Text+"')";
cm = new SqlCommand(strInsert,cn);
cn.Open();
try
{
cm.ExecuteNonQuery();
message.InnerHtml="<b>注册成功!请记住你的ID已备下次使用!</b>";
}
catch(Exception ee)
{
message.InnerHtml=ee.ToString();//捕捉错误信息。
}
cn.Close();
}
#endregion
zhjboss 2004-04-14
  • 打赏
  • 举报
回复
string strcon="data source=zhangjing;uid=sa;pwd=@#$zhj258654;initial catalog=zhjcar";
SqlConnection myconn;
放在前面声明哪里
private void button7_Click(object sender, System.EventArgs e)
{
try
{
SqlConnection myconn=new SqlConnection(strcon);
string strsquer="select car_id as 车牌,car_type as 车型, start_place as 始发站,end_place as 终点站,start_time as 发车时间, time1 as [所用时间(小时)],distance as [距离(米)], price as 票价,seats as 座位 from carmain ";
SqlCommand mycom=new SqlCommand(strsquer,myconn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=mycom;
myconn.Open();
DataSet ds = new DataSet ( );
da.Fill(ds,"carmain");
dataGrid1.DataSource =ds;
dataGrid1.SetDataBinding(ds,"carmain");
myconn.Close ( ) ;
da.Dispose ( ) ;
}
catch(Exception ee)
{
MessageBox.Show("出错了"+ee.ToString());
}
}
smoothwood 2004-04-13
  • 打赏
  • 举报
回复
我的代码是VB.NET的,不能放在.aspx.cs中哟,.aspx.cs是用C#写的程序:)
VB.NET写的应该是放在.aspx.vb中

恩。。。放在比如Page_Load事件或者Button_Click之类的事件中
jeall 2004-04-13
  • 打赏
  • 举报
回复
放在.aspx.cs的代码中,
比如,你想在PageLoad中进行数据邦定就放在PageLoad中,总之,放在你需要绑定数据的地方!
alderchew 2004-04-13
  • 打赏
  • 举报
回复
想问一下,这一段code 要放哪里?
smoothwood 2004-04-13
  • 打赏
  • 举报
回复
恩。。。改一点!
看下面这段代码
————————————————————————————————————
Dim conNorthwind As SqlConnection
Dim daNorthwind As SqlDataAdapter
dsStore = New DataSet
conNorthwind = New SqlConnection("Server=localhost;user id=sa;password=xxx;database=Northwind;")
daNorthwind = New SqlDataAdapter("Select * from Categories", conNorthwind)
daNorthwind.Fill(dsStore, "Categories")
datagrid1.datasource=dsStore.Tables("Categories")
datagrid1.DataBind()


以上代码是将Northwind数据库中Categories表中的数据(的内容查询出来填充到DataSet中,并将数据显示在DataGrid控件中
smoothwood 2004-04-13
  • 打赏
  • 举报
回复
Dim conNorthwind As SqlConnection
Dim daNorthwind As SqlDataAdapter
dsStore = New DataSet
conNorthwind = New SqlConnection("Server=localhost;user id=sa;password=xxx;database=Northwind;")
daNorthwind = New SqlDataAdapter("Select CategoryID,categoryName from Categories", conNorthwind)
daNorthwind.Fill(dsStore, "Categories")
DataList1.DataSource = dsStore.Tables("Categories")
DataList1.DataBind()

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧