当前位置:首页 » 操作系统 » 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();//打开连接
热点内容
中国版ec服务器ip 发布:2024-09-25 19:22:32 浏览:737
安卓蜂窝数据哪里打开 发布:2024-09-25 19:21:41 浏览:204
c语言挖洞 发布:2024-09-25 18:56:39 浏览:925
方舟怎么在服务器公屏发消息 发布:2024-09-25 18:48:38 浏览:366
幻塔安卓官服哪里下 发布:2024-09-25 18:47:11 浏览:906
安卓手机无线网怎么增强 发布:2024-09-25 18:46:59 浏览:494
md5加密php代码 发布:2024-09-25 18:39:54 浏览:127
gdb83编译 发布:2024-09-25 18:34:44 浏览:520
为什么安卓不用方舟编译器 发布:2024-09-25 18:28:52 浏览:9
56上传视频赚钱 发布:2024-09-25 18:14:06 浏览:616