我如何在ASP.Net中使用数据库

时间:2022-02-08 14:29:34

I've developed web applications in PHP for a few years and would like to learn about ASP.Net. I've installed VS2013 and have created an ASP.Net Web Application. I tried playing around with something that I found on wait for it W3Schools just because I knew it would be as simple as simple could be but it caused me some errors. I was trying to "connect" to an Access file in the wwwroot directory by using System.Data.OleDb but I had some problems.

我用PHP开发了几年的Web应用程序,想了解ASP.Net。我安装了VS2013并创建了一个ASP.Net Web应用程序。我试着玩弄我在W3Schools上找到的东西只是因为我知道它会像简单一样简单,但它却给我带来了一些错误。我试图通过使用System.Data.OleDb“连接”到wwwroot目录中的Access文件,但我遇到了一些问题。

My question is: Is there a simplistic way like in PHP where you have PHPMyAdmin to manage the database and then connect via something simple like $conn = new mysqli('localhost', 'user', 'password', 'db'); but for ASP.Net?

我的问题是:在PHP中是否存在一种简单的方式,您可以使用PHPMyAdmin来管理数据库,然后通过简单的连接来连接,例如$ conn = new mysqli('localhost','user','password','db');但是对于ASP.Net?

I'm struggling to find beginner level support for this on the web and would like to figure it out asap!

我很难在网上找到初学者级别的支持,并想尽快解决它!

1 个解决方案

#1


2  

David, isnt's going to be "simple" as PHP, remember that VS2013 it's a server side language, more strong and complex. I recommend to you the next: Work with objects. Here is some code may help you.

大卫,不会像PHP那样“简单”,请记住VS2013它是一种服务器端语言,更强大,更复杂。我建议你下一个:使用对象。这里有一些代码可以帮到你。

C#:

C#:

public System.Data.DataSet GetQuery(string _QueryComm){
System.Data.DataSet objResult = new System.Data.DataSet();
OleDbDataAdapter objAdapter;

strProvider = "Provider=SQLOLEDB.1;Data Source=YourServer;Initial Catalog=Database;User Id=databaseuser;Password=pass;";
objCon = new OleDbConnection(strProvider);
objCon.Open();
try
{
    objAdapter = new OleDbDataAdapter(_QueryComm, objCon);
    objAdapter.Fill(objResult);
    objAdapter.Dispose();
    objCon.Close();
}
catch (Exception e)
{
    // Some exception handler
}
return objResult;}

Usage:

用法:

DataSet datainfo = GetQuery("select * from table");

VB:

VB:

Public Function GetQuery(strCommandQuery as String) As System.Data.DataSet
Dim objResult As System.Data.DataSet = New System.Data.DataSet
Dim objAdapter As OleDbDataAdapter

strProvider = "Provider=SQLOLEDB.1;Data Source=YourServer;Initial Catalog=Database;User Id=databaseuser;Password=pass;"

objCon = New OleDbConnection(strProvider)
objCon.Open()

Try
    objAdapter = New OleDbDataAdapter(strCommandQuery, objCon)
    objAdapter.Fill(objResult)
    objAdapter.Dispose()
    objCon.Close()
Catch ex As System.Exception
    ' Some exception handler
End Try

Return objResult  End Function

Usage:

用法:

Dim datainfo as DataSet = GetQuery("select * From table")

Let me know if it's work for you.

让我知道它是否适合你。

#1


2  

David, isnt's going to be "simple" as PHP, remember that VS2013 it's a server side language, more strong and complex. I recommend to you the next: Work with objects. Here is some code may help you.

大卫,不会像PHP那样“简单”,请记住VS2013它是一种服务器端语言,更强大,更复杂。我建议你下一个:使用对象。这里有一些代码可以帮到你。

C#:

C#:

public System.Data.DataSet GetQuery(string _QueryComm){
System.Data.DataSet objResult = new System.Data.DataSet();
OleDbDataAdapter objAdapter;

strProvider = "Provider=SQLOLEDB.1;Data Source=YourServer;Initial Catalog=Database;User Id=databaseuser;Password=pass;";
objCon = new OleDbConnection(strProvider);
objCon.Open();
try
{
    objAdapter = new OleDbDataAdapter(_QueryComm, objCon);
    objAdapter.Fill(objResult);
    objAdapter.Dispose();
    objCon.Close();
}
catch (Exception e)
{
    // Some exception handler
}
return objResult;}

Usage:

用法:

DataSet datainfo = GetQuery("select * from table");

VB:

VB:

Public Function GetQuery(strCommandQuery as String) As System.Data.DataSet
Dim objResult As System.Data.DataSet = New System.Data.DataSet
Dim objAdapter As OleDbDataAdapter

strProvider = "Provider=SQLOLEDB.1;Data Source=YourServer;Initial Catalog=Database;User Id=databaseuser;Password=pass;"

objCon = New OleDbConnection(strProvider)
objCon.Open()

Try
    objAdapter = New OleDbDataAdapter(strCommandQuery, objCon)
    objAdapter.Fill(objResult)
    objAdapter.Dispose()
    objCon.Close()
Catch ex As System.Exception
    ' Some exception handler
End Try

Return objResult  End Function

Usage:

用法:

Dim datainfo as DataSet = GetQuery("select * From table")

Let me know if it's work for you.

让我知道它是否适合你。