如何在我的asp.net网站发布时连接我的sql数据库进行查询 [英] How to connect my sql database to query when published my asp.net website

查看:45
本文介绍了如何在我的asp.net网站发布时连接我的sql数据库进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用sql server发布我的asp.net网站。现在我需要知道是否有什么知道如何连接sql数据库和我的.aspx文件。

在App_Data foleder我把我的sql数据库(.mdf)。

Plz帮帮我。

I need to published my asp.net website with sql server. Now I need to know if there is something to know how to connect sql database with my .aspx file.
In App_Data foleder I put my sql database(.mdf).
Plz help me.

推荐答案

如果使用专用的ip,你必须将该数据库附加到共享的sql server数据库或在sql server中创建一个数据库。



对于本地主机,将数据库放在本地sql server中是很好的。

但是在发布到外部URL时(或者共享ir专用)服务器),您需要确保在共享服务器或专用服务器中创建了数据库。



注意:所有共享服务器都没有sql server。所以请与您的服务提供商进行交叉核对。



希望这会有所帮助。

欢呼
You have to attach that database to the shared sql server database or create a database in your sql server if using dedicated ip.

For local host, it is well good to have your database in your local sql server.
But while publishing to external url (either shared ir dedicated server), you need to ensure that you have created your database in the shared server or dedicated server.

Note: All shared server does not have sql server. So cross check with your Service Provider.

Hope this helps.
cheers


你可以将本地mdf文件发布到目标服务器。应相应地更改连接字符串/设置。请参阅以下有关如何操作的信息。



http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/deploying-a-database-cs [ ^ ]



如果卡在某处,请尝试恢复。

希望这有帮助
You can publish the local mdf file to your target server . The connection string/settings should be changed accordingly. Refer the following on how to do it.

http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/deploying-a-database-cs[^]

Try and revert if stuck somewhere.
Hope this helps


您可以从mysql网站安装Connector / Net 1.0(.NET):

< br $>
http://www.mysql.com/downloads/connector/net/



并转移MySql.Data.dll,MySql。 Data.Entity.dll和MySql.Web.dll到你项目的bin文件夹。



这是你可以用来连接和最终显示的新代码你的数据库在线。

这适用于godaddy:

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls ;



//包含MySql .Net连接器的命名空间

使用MySql.Data.MySqlClient;

使用System.Data;

使用System.Data.SqlClient;





命名空间WebApplication1

{

public partial class _Default:System.Web.UI。页面

{

protected void Page_Load(object sender,EventArgs e)

{

string connectionString =

Server = ####。db。####。####。com; +

Port = ####;数据库= ####; UID = ####; Pwd = ####;;



//创建连接对象

MySqlConnection mySqlConnection = new MySqlConnection(connectionString);



//创建连接命令

MySqlCommand mySqlCommand = new MySqlCommand(SELECT * FROM mytable,mySqlConnection);



//检查连接状态并相应地打开它。

if(mySqlConnection.State == ConnectionState.Closed)

mySqlConnection。 Open();



//从SQL Server数据库中读取行流的MySql datareader对象

MySqlDataReader myDataReader = mySqlCommand.ExecuteReader( ); $



//将Sql DataReader对象传递给DataSource属性

// DataList控件以呈现项目列表。

myDataG rid.DataSource = myDataReader;

myDataGrid.DataBind();



//关闭Sql DataReader对象

myDataReader.Close();



//检查连接状态并相应地关闭它。

if(mySqlConnection.State == ConnectionState 。打开)

mySqlConnection.Close();



}



}

}
You can install the "Connector/Net 1.0 (.NET)" from the mysql website:

http://www.mysql.com/downloads/connector/net/

and transfer the MySql.Data.dll, MySql.Data.Entity.dll, and MySql.Web.dll to the bin folder of your project.

Here is the new code that you can use to connect and finaly display your database online.
This works on godaddy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//Include namespaces for the MySql .Net Connector
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;


namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString =
"Server=####.db.####.####.com; " +
"Port=####; Database=####; Uid=####; Pwd=####;";

//Create a connection object
MySqlConnection mySqlConnection = new MySqlConnection(connectionString);

//Create a connection command
MySqlCommand mySqlCommand = new MySqlCommand("SELECT * FROM mytable", mySqlConnection);

//check the connection state and open it accordingly.
if (mySqlConnection.State == ConnectionState.Closed)
mySqlConnection.Open();

//MySql datareader object to read the stream of rows from SQL Server Database
MySqlDataReader myDataReader = mySqlCommand.ExecuteReader();

//Pass the Sql DataReader object to the DataSource property
//of DataList control to render the list of items.
myDataGrid.DataSource = myDataReader;
myDataGrid.DataBind();

// close the Sql DataReader object
myDataReader.Close();

// check the connection state and close it accordingly.
if (mySqlConnection.State == ConnectionState.Open)
mySqlConnection.Close();

}

}
}


这篇关于如何在我的asp.net网站发布时连接我的sql数据库进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆