當前位置:首頁 » 操作系統 » aspnet導入資料庫

aspnet導入資料庫

發布時間: 2022-03-30 06:10:58

A. aspnet從excel讀入資料庫sql,sqldatareader對象為null,為什麼

貼圖貼代碼

B. IIS 配置ASP.NET 連接資料庫問題

與資料庫連接失敗啊~你看看sqlserver的服務有沒有開啟,開啟了的話,看資料庫SA用戶的密碼對不對?甚至說你都沒有用SA用戶?你網頁連接資料庫用的什麼用戶?是WINDOWS信任連接還是ASPNET內置用戶?用SQL Server Management Studio Express進入資料庫,在安全性一欄看裡面的用戶是否是有連接資料庫的用戶?有的話看看許可權加了沒有?有沒有做用戶映射?

把你的連接貼出來看看,或者就如上位講的,是DATA SOURCE的指向問題

C. 在ASP。NET中如何向資料庫中插入圖片

這 個你要簡單的是吧 就是 把圖片另存到一個路徑下的指定文件夾下即可,ASP.NET代碼如下:
string file = FileUpload1.FileName;//獲取文件名
//實例化一個隨機對象
Random rd = new Random();
string time= DateTime.Now.Ticks.ToString();//
int a=rd.Next();
int b=file.LastIndexOf(".");
string s = file.Substring(b).ToString() ;
string name = time + a.ToString() + s;
//Server.MapPath("upload" + name);
string qq= Server.MapPath("upload/");
string zz = qq + name;
FileUpload1.SaveAs(zz);//將圖片另存為

D. asp.net連接資料庫實例

Sub LoadCommandList()
Dim objConn As New Odbc.OdbcConnection
Dim objCmd As New Odbc.OdbcCommand
Dim objDataReader As Odbc.OdbcDataReader
CommandList.Items.Clear()
objConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & Application.StartupPath & "\database.mdb"
Debug.Print(Application.StartupPath & "\database.mdb")
objConn.Open()'連接資料庫
objCmd.Connection = objConn
objCmd.CommandText = "select * from Commands"
objDataReader = objCmd.ExecuteReader()'執行SQL語句
While objDataReader.Read()'讀數據,以下和資料庫連接無關
CommandList.Items.Add(objDataReader.GetString(0) & "," & objDataReader.GetString(1))
End While
objConn.Close()
End Sub



Asp.net連接SQL Server2000資料庫常式詳解:
<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.SqlClient" %>
<script laguage="VB" runat="server">
sub page_load(sender as Object,e as EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ds as DataSet
'1.連接資料庫
myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
myConnection.Open()
la1.text="Connection Opened!"

'2.建立一個新表
myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL )", myConnection )
myCommand.ExecuteNonQuery()
la2.text="New table created!"

'2 添加紀錄
myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黃志文','男' )", myConnection )
myCommand.ExecuteNonQuery()
la3.text="New Record Inserted!"

'3 更新數據
myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection )
myCommand.ExecuteNonQuery()
la4.text="Record Updated!"

'4 刪除數據
myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection )
myCommand.ExecuteNonQuery()
la5.text="Record Deleted!"

'5 用DataGrid顯示數據
myCommand = New SqlCommand( "select * from [test]", myConnection )
MyDataGrid.DataSource=myCommand.ExecuteReader()
MyDataGrid.DataBind()
end sub
</script>
<html>
<body>
<asp:label id="la1" runat="server" /><br>
<asp:label id="la2" runat="server" /><br>
<asp:label id="la3" runat="server" /><br>
<asp:label id="la4" runat="server" /><br>
<asp:label id="la5" runat="server" /><br>
<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="10pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
>
</asp:DataGrid>

</body>
</html>
----------------------------------------------------------------------------------------------------
ASP.net連接access資料庫常式
<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.OleDb" %>
<script laguage="VB" runat="server">
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
sub page_load(sender as Object,e as EventArgs)
'1.連接資料庫
dim dbname as string
dbname=server.mappath("authors.mdb")
myConnection = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="&dbname )
myConnection.Open()
la1.text="Connection Opened!"

'2.添加記錄
myCommand = New OleDbCommand( "Insert INTO Authors(Authors,country) Values('Simson','usa')", myConnection )
myCommand.ExecuteNonQuery()
la2.text="New Record Inserted!"

'3 更新數據(Access)
myCommand = New OleDbCommand( "UPDATE Authors SET Authors='Bennett' WHERE Authors = 'Simson'", myConnection )
myCommand.ExecuteNonQuery()
la3.text="Record Updated!"

'4 刪除數據(access)
myCommand = New OleDbCommand( "DELETE FROM Authors WHERE Authors = 'David'", myConnection )
myCommand.ExecuteNonQuery()
la4.text="Record Deleted!"

'5 使用DateGrid顯示數據
myCommand = New OleDbCommand( "select * FROM Authors", myConnection )
MyDataGrid.DataSource=myCommand.Executereader()
MyDataGrid.DataBind()

end sub
</script>
<html>
<body>
<asp:label id="la1" runat="server" /><br>
<asp:label id="la2" runat="server" /><br>
<asp:label id="la3" runat="server" /><br>
<asp:label id="la4" runat="server" /><br>
<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="10pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
>
</asp:DataGrid>

</body>
</html>
1.C#連接連接Access

[復制此代碼]CODE:

using System.Data;
using System.Data.OleDb;
..
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+=@"Data Source=C:BegASPNETNorthwind.mdb";
OleDbConnection objConnection=new OleDbConnection(strConnection);
..
objConnection.Open();
objConnection.Close();

解釋:
連接Access資料庫需要導入額外的命名空間,所以有了最前面的兩條using命令,這是必不可少的!
strConnection這個變數里存放的是連接資料庫所需要的連接字元串,他指定了要使用的數據提供者和要使用的數據源.
"Provider=Microsoft.Jet.OleDb.4.0;"是指數據提供者,這里使用的是Microsoft Jet引擎,也就是Access中的數據引擎,asp.net就是靠這個和Access的資料庫連接的.
"Data Source=C:\BegASPNET\Northwind.mdb"是指明數據源的位置,他的標准形式是"Data Source=MyDrive:MyPath\MyFile.MDB".
PS:
1."+="後面的"@"符號是防止將後面字元串中的"\"解析為轉義字元.
2.如果要連接的資料庫文件和當前文件在同一個目錄下,還可以使用如下的方法連接:
strConnection+="Data Source=";
strConnection+=MapPath("Northwind.mdb");
這樣就可以省得你寫一大堆東西了!
3.要注意連接字元串中的參數之間要用分號來分隔.
"OleDbConnection objConnection=new OleDbConnection(strConnection);"這一句是利用定義好的連接字元串來建立了一個鏈接對象,以後對資料庫的操作我們都要和這個對象打交道.
"objConnection.Open();"這用來打開連接.至此,與Access資料庫的連接完成.
2.C#連接SQL Server

[復制此代碼]CODE:

using System.Data;
using System.Data.SqlClient;
..
string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=30";
SqlConnection objConnection=new SqlConnection(strConnection);
..
objConnection.Open();
objConnection.Close();

解釋:
連接SQL Server資料庫的機制與連接Access的機制沒有什麼太大的區別,只是改變了Connection對象和連接字元串中的不同參數.
首先,連接SQL Server使用的命名空間不是"System.Data.OleDb",而是"System.Data.SqlClient".
其次就是他的連接字元串了,我們一個一個參數來介紹(注意:參數間用分號分隔):
"user id=sa":連接資料庫的驗證用戶名為sa.他還有一個別名"uid",所以這句我們還可以寫成"uid=sa".
"password=":連接資料庫的驗證密碼為空.他的別名為"pwd",所以我們可以寫為"pwd=".
這里注意,你的SQL Server必須已經設置了需要用戶名和密碼來登錄,否則不能用這樣的方式來登錄.如果你的SQL Server設置為Windows登錄,那麼在這里就不需要使用"user id"和"password"這樣的方式來登錄,而需要使用"Trusted_Connection=SSPI"來進行登錄.
"initial catalog=Northwind":使用的數據源為"Northwind"這個資料庫.他的別名為"Database",本句可以寫成"Database=Northwind".
"Server=YourSQLServer":使用名為"YourSQLServer"的伺服器.他的別名為"Data Source","Address","Addr".如果使用的是本地資料庫且定義了實例名,則可以寫為"Server=(local)\實例名";如果是遠程伺服器,則將"(local)"替換為遠程伺服器的名稱或IP地址.
"Connect Timeout=30":連接超時時間為30秒.
在這里,建立連接對象用的構造函數為:SqlConnection.
3.C#連接Oracle

[復制此代碼]CODE:

using System.Data.OracleClient;
using System.Data;
//在窗體上添加一個按鈕,叫Button1,雙擊Button1,輸入以下代碼
private void Button1_Click(object sender, System.EventArgs e)
{
string ConnectionString="Data Source=sky;user=system;password=manager;";//寫連接串
OracleConnection conn=new OracleConnection(ConnectionString);//創建一個新連接
try
{
conn.Open();
OracleCommand cmd=conn.CreateCommand();
cmd.CommandText="select * from MyTable";//在這兒寫sql語句
OracleDataReader odr=cmd.ExecuteReader();//創建一個OracleDateReader對象
while(odr.Read())//讀取數據,如果odr.Read()返回為false的話,就說明到記錄集的尾部了
{
Response.Write(odr.GetOracleString(1).ToString());//輸出欄位1,這個數是欄位索引,具體怎麼使用欄位名還有待研究
}
odr.Close();
}
catch(Exception ee)
{
Response.Write(ee.Message); //如果有錯誤,輸出錯誤信息
}
finally
{
conn.Close(); //關閉連接
}
}

4.C#連接MySQL

[復制此代碼]CODE:

using MySQLDriverCS;
// 建立資料庫連接
MySQLConnection DBConn;
DBConn = new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString);
DBConn.Open();
// 執行查詢語句
MySQLCommand DBComm;
DBComm = new MySQLCommand("select Host,User from user",DBConn);
// 讀取數據
MySQLDataReader DBReader = DBComm.ExecuteReaderEx();
// 顯示數據
try
{
while (DBReader.Read())
{
Console.WriteLine("Host = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
}
}
finally
{
DBReader.Close();
DBConn.Close();
}
//關閉資料庫連接
DBConn.Close();

5.C#連接IBM DB2

[復制此代碼]CODE:

OleDbConnection1.Open();
//打開資料庫連接
OleDbDataAdapter1.Fill(dataSet1,"Address");
//將得來的數據填入dataSet
DataGrid1.DataBind();
//綁定數據
OleDbConnection1.Close();
//關閉連接
//增加資料庫數據

在Web Form上新增對應欄位數量個數的TextBox,及一個button,為該按鍵增加Click響應事件代碼如下:

[復制此代碼]CODE:

this.OleDbInsertCommand1.CommandText = "INSERTsintosADDRESS(NAME,
EMAIL, AGE, ADDRESS) VALUES
('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
OleDbInsertCommand1.Connection.Open();
//打開連接
OleDbInsertCommand1.ExecuteNonQuery();
//執行該SQL語句
OleDbInsertCommand1.Connection.Close();
//關閉連接

6.C#連接SyBase
(OleDb)

[復制此代碼]CODE:

Provider=Sybase.ASEOLEDBProvider.2;Initial Catalog=資料庫名;User ID=用戶名;Data Source=數據源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000;

E. ASP.NET連接資料庫有哪幾種方法

連接Access
首先看一個例子代碼片斷:
程序代碼:
--------------------------------------------------------------------------------
using
System.Data;
using
System.Data.OleDb;
......
string
strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+=@"Data
Source=C:\BegASPNET\Northwind.mdb";
OleDbConnection
objConnection=new
OleDbConnection(strConnection);
......
objConnection.Open();
objConnection.Close();
......
--------------------------------------------------------------------------------
解釋:
連接Access資料庫需要導入額外的命名空間,所以有了最前面的兩條using命令,這是必不可少的!
strConnection這個變數里存放的是連接資料庫所需要的連接字元串,他指定了要使用的數據提供者和要使用的數據源.
"Provider=Microsoft.Jet.OleDb.4.0;"是指數據提供者,這里使用的是Microsoft
Jet引擎,也就是Access中的數據引擎,asp.net就是靠這個和Access的資料庫連接的.
"Data
Source=C:\BegASPNET\Northwind.mdb"是指明數據源的位置,他的標准形式是"Data
Source=MyDrive:MyPath\MyFile.MDB".
ps:
1."+="後面的"@"符號是防止將後面字元串中的"\"解析為轉義字元.
2.如果要連接的資料庫文件和當前文件在同一個目錄下,還可以使用如下的方法連接:
strConnection+="Data
Source=";
strConnection+=MapPath("Northwind.mdb");
這樣就可以省得你寫一大堆東西了!
3.要注意連接字元串中的參數之間要用分號來分隔.
"OleDbConnection
objConnection=new
OleDbConnection(strConnection);"這一句是利用定義好的連接字元串來建立了一個鏈接對象,以後對資料庫的操作我們都要和這個對象打交道.
"objConnection.Open();"這用來打開連接.至此,與Access資料庫的連接完成.其餘操作(插入,刪除...)請參閱相關書籍
連接SQL
Server
例子代碼片斷:
程序代碼:
--------------------------------------------------------------------------------
using
System.Data;
using
System.Data.SqlClient;
...
string
strConnection="user
id=sa;password=;";
strConnection+="initial
catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect
Timeout=30";
SqlConnection
objConnection=new
SqlConnection(strConnection);
...
objConnection.Open();
objConnection.Close();
...
--------------------------------------------------------------------------------
解釋:
連接SQL
Server資料庫的機制與連接Access的機制沒有什麼太大的區別,只是改變了Connection對象和連接字元串中的不同參數.
首先,連接SQL
Server使用的命名空間不是"System.Data.OleDb",而是"System.Data.SqlClient".
其次就是他的連接字元串了,我們一個一個參數來介紹(注意:參數間用分號分隔):
"user
id=sa":連接資料庫的驗證用戶名為sa.他還有一個別名"uid",所以這句我們還可以寫成"uid=sa".
"password=":連接資料庫的驗證密碼為空.他的別名為"pwd",所以我們可以寫為"pwd=".
這里注意,你的SQL
Server必須已經設置了需要用戶名和密碼來登錄,否則不能用這樣的方式來登錄.如果你的SQL
Server設置為Windows登錄,那麼在這里就不需要使用"user
id"和"password"這樣的方式來登錄,而需要使用"Trusted_Connection=SSPI"來進行登錄.
"initial
catalog=Northwind":使用的數據源為"Northwind"這個資料庫.他的別名為"Database",本句可以寫成"Database=Northwind".
"Server
=YourSQLServer":使用名為"YourSQLServer"的伺服器.他的別名為"Data
Source","Address","Addr".如果使用的是本地資料庫且定義了實例名,則可以寫為"Server=(local)\實例名";如果是遠程伺服器,則將"(local)"替換為遠程伺服器的名稱或IP地址.
"Connect
Timeout=30":連接超時時間為30秒.
在這里,建立連接對象用的構造函數為:SqlConnection.
其餘的就和Access沒有什麼區別了!

F. 使用aspnet將word表單導入系統中形成web表單,在系統中完成web表單,然後將web表單再導出到word

web表單要有個東西來接收
用Grid
DataSetOpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel files(*.xls)|*.xls";ExcelIO excelio = new ExcelIO();if(openFile.ShowDialog()==DialogResult.OK)
{
if(excelio!=null)
excelio.Close(); excelio = new ExcelIO(openFile.FileName);
object[,] range = excelio.GetRange();
excelio.Close();
DataSet ds = new DataSet("xlsRange"); int x = range.GetLength(0);
int y = range.GetLength(1); DataTable dt = new DataTable("xlsTable");
DataRow dr;
DataColumn dc;

ds.Tables.Add(dt); for(int c=1; c<=y; c++)
{
dc = new DataColumn();
dt.Columns.Add(dc);
}

object[] temp = new object[y];

for(int i=1; i<=x; i++)
{
dr = dt.NewRow(); for(int j=1; j<=y; j++)
{
temp[j-1] = range[i,j];
}

dr.ItemArray = temp;
ds.Tables[0].Rows.Add(dr);
} dataGrid1.SetDataBinding(ds,"xlsTable");

if(excelio!=null)
excelio.Close();
}

//導出
private void ExportToWord(DataTable dt, string targetWordFile)
{
object oMissing = System.Reflection.Missing.Value;

//Start Word and create a new document.
Word._Application oWord = null;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = dt.Rows[0]["mname"].ToString(); //會議名稱!
oPara1.Range.Font.Bold = 1;
oPara1.Range.Font.Size = 18;
oPara1.Range.Font.Color = Word.WdColor.wdColorRed;
oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();

object oMissing2 = System.Reflection.Missing.Value;

//Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing2);
oPara2.Range.Text = dt.Rows[0]["datetime"].ToString(); //會議時間
oPara2.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
oPara1.Range.Font.Color = Word.WdColor.wdColorBlack;
oPara2.Range.Font.Bold = 1;
oPara2.Range.Font.Size = 14;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();

int rowcount = dt.Rows.Count + 1; // 含標題行,及所有用戶行 表格總行//Insert a 3 x 4 table, fill it with data, and make the first row
//bold and italic.
//Insert another paragraph.
Word.Paragraph oPara4;
oPara4 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara4.Format.SpaceAfter = 24;
oPara4.Range.Font.Bold = 0;
oPara4.Range.Font.Size = 12;
oPara4.Range.InsertParagraphAfter();

Word.Table oTable;
Word.Range wrdRng = oPara4.Range;

G. 在asp.net中,無法添加資料庫鏈接,怎麼解決呢

1、打開SDK 命令提示(所有程序——Microsoft .NET Framework SDK v2.0——SDK 命令提示。也可打開VS的命令提示),輸入aspnet_regsql,彈出ASP.NET SQL Server 安裝向導,點下一步,點「為應用程序服務配置 SQL Server」,點下一步,資料庫用<默認>(aspnetdb),點下一步,完成。
2、打開Visual Studio 2005,依次:工具-->選項-->資料庫工具-->數據連接-->SQL Server實例名稱(默認為空),改為你的伺服器名稱(默認實例的名稱即為你的計算機名稱)。
3、還是Visual Studio 2005,工具-->連接到資料庫-->伺服器名(輸入剛才的伺服器),可以按你要求選擇Windows或SQL Server身份驗證,然後資料庫選擇剛才的aspnetdb。測試OK後,點「高級」,復制對話窗口的最下面一行語句(比如,如果你之前選擇「使用SQL Server身份驗證」,則復制出來的語句類似為Data Source=Server;Initial Catalog=aspnetdb;User ID=sa)
4、打開IIS:默認網站-->屬性-->ASP.NET-->編輯全局配置-->常規-->點擊「連接字元串管理器」的「LocalSqlServer」後,點編輯,然後清除裡面的字元串,再粘貼第3步所復制的字元串,如果你第3步是選擇SQL Server身份驗證的,還需在後邊再手動輸入「;Password=sa」,當然,後面的sa用你的密碼替換,然後確定,如果第3步是Windows身份驗證的,則粘貼後直接確定保存即可 -->應用。(如果第3步是選擇SQL Server身份驗證的,則修改後的連接字元串類似為Data Source=Server;Initial Catalog=aspnetdb;User ID=sa;Password=sa)
5、還是在IIS:默認網站-->屬性-->ASP.NET-->編輯全局配置-->身份驗證-->選定"啟用角色管理"-->確定。
按照上面的步驟,到第2步時就無法完成,提示sql server不存在。突然想到,會不會sql server express服務還沒啟動?打開服務管理,果然。手動啟動它,又提示啟動失敗,請查看日誌。打開管理工具中的事件查看器,錯誤信息:
文件 "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\mastlog.ldf" 已壓縮,但未駐留在只讀資料庫或文件組中。必須將此文件解壓縮。

H. 誰能給一個ASPNET連接資料庫的通用類代碼

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// DataBase 的摘要說明
/// </summary>
public class DataBase
{

public DataBase()
{
//
// TODO: 在此處添加構造函數邏輯
//
}

public static SqlConnection ReturnConn()
{
string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection Conn = new SqlConnection(strConn);
if (Conn.State.Equals(ConnectionState.Closed))
{
Conn.Open();
}
return Conn;
}
public static SqlCommand CreateCmd(string procName, SqlParameter[] prams, SqlConnection Conn)
{
SqlConnection SqlConn = Conn;
if (SqlConn.State.Equals(ConnectionState.Closed))
{
SqlConn.Open();
}
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcere;
Cmd.Connection = SqlConn;
Cmd.CommandText = procName;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if (parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}

public static int RunExecute(string procName, SqlParameter[] prams)
{
SqlConnection Conn = ReturnConn();
SqlCommand Cmd = CreateCmd(procName, prams, Conn);
int intResult = Cmd.ExecuteNonQuery();
Conn.Close();
return intResult;
}

}
你把你的郵箱告訴我 我給你吧

I. 如何將ASPNET網頁中的數據更新到資料庫的表中其中表已經定義。

protected void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection("Data Source="資料庫IP地址";Initial Catalog = "資料庫名稱";User ID="用戶名";Password="密碼";Connect Timeout=60");//資料庫連接
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
string sql = "insert investigation (欄位1名稱,欄位2名稱) values('"+Textbox1.Text+"','"+Textbox2.Text+"')";
cmd.CommandText = sql;//要執行的sql語句
cmd.Connection = cn;//傳入資料庫連接參數
cn.Open();//打開資料庫連接
if(cmd.ExecuteNonQuery())//執行cmd.CommandText中的sql
{
//成功時執行
}
else
{
//失敗時執行
}
}

頁面中加入
<form id="form1" runat="server">
<asp:TextBox ID="Textbox3" runat="server"></asp:TextBox>
<asp:TextBox ID="Textbox4" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>

J. ASP怎麼連接SQL資料庫

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data.SqlClient;//注意需要添加此句

namespaceaspnet3
{
publicpartialclassdatatest:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
stringstrconn="server=localhost;uid=sa;pwd=longlt;database=School";
SqlConnectionconn=newSqlConnection(strconn);//創建連接
stringsql="select*fromstudents";
conn.Open();
SqlCommandcmd=newSqlCommand(sql,conn);//執行查詢
Response.Write("連接成功");
SqlDataReaderdr=cmd.ExecuteReader();//查詢結果
if(dr.Read())
{
//利用dr[索引]對資料庫表進行操作,dr[]返回object;
//可以用欄位做索引,也可用列號0,1..做索引
Response.Write(dr[0].ToString()+"<br>");
}

//this.Lab.Text="suc";
}
}
}

在上面的例子中,我們連接了一個sa下的School資料庫,並查詢了其中students欄位的內容。

連接資料庫分為三個步驟:先定義連接信息,再創建一個連接,最後打開連接

stringstrconn="server=localhost;uid=sa;pwd=longlt;database=School";//在這一段修改資料庫的信息
SqlConnectionconn=newSqlConnection(strconn);//創建連接
conn.Open();//打開連接
熱點內容
js判斷是否是手機訪問 發布:2024-09-25 21:13:59 瀏覽:429
中電興發磁光電存儲生產線 發布:2024-09-25 21:02:16 瀏覽:785
安卓手機怎麼弄蘋果照片水印 發布:2024-09-25 21:01:09 瀏覽:906
f在c語言中是什麼意思 發布:2024-09-25 20:49:10 瀏覽:818
垃圾清除緩存什麼意思 發布:2024-09-25 20:39:53 瀏覽:987
我的世界手機國際版開伺服器地址 發布:2024-09-25 20:27:47 瀏覽:343
短視頻腳本書寫 發布:2024-09-25 20:02:22 瀏覽:679
列印伺服器ip更改 發布:2024-09-25 20:02:21 瀏覽:176
python首字母 發布:2024-09-25 19:49:58 瀏覽:59
微信小程序的數據存儲 發布:2024-09-25 19:49:22 瀏覽:873