用Visual Studio开发web应用程序,自带的.dbo后台数据库如何用代码实现连接

调用数据连接两种方法1.web.config (配置文件) 中<appSettings><add key="ConnectionString" value="server=localhost\sqlexpress;uid=sa;pwd=123456;database=News"/></appSettings>调用的时候string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();SqlConnection Conn = new SqlConnection(strConn);2.或者不用web.config直接在文件中写SqlConnection conn = new SqlConnection("server=.\\SQLEXPRESS;uid=sa;pwd=123456;database=login");如果写成字符串一定要注意,需要写一个转义字符 结果就是 .\\SqlExpress如何是Express版的数据库,一定要在服务器名的后面加上 \\SSQLEXPRESS一个完整的例子   string userName = Request.Form["userName"];         string userPwd = Request.Form["userPwd"];         SqlConnection con = new SqlConnection("server=localhost\\SqlExpress;uid=sa;pwd=123456;database=login");         con.Open();         SqlCommand cmd=new SqlCommand("select count(*) from login where userName='"+userName+"' and userPwd='"+userPwd+"'",con);         int count=Convert.ToInt32(cmd.ExecuteScalar());         if(count>0)         {         Response.Redirect("main.aspx");         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较大的项目,我尽可能详细地给出步骤和代码。 1. 创建数据库 首先,我们需要创建一个数据库来存储我们的数据。假设我们要创建一个学生信息管理系统,我们可以创建一个名为"StudentManagement"的数据库。可以使用SQL Server Management Studio或者Visual Studio自带的Server Explorer进行创建。 2. 创建表 在创建完数据库之后,我们需要创建表来存储具体的数据。我们可以创建一个名为"Student"的表,用来存储学生的信息。表中应该包含以下字段:学生ID、姓名、性别、年龄、班级、联系方式等。 CREATE TABLE [dbo].[Student]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, [Gender] [nvarchar](10) NOT NULL, [Age] [int] NOT NULL, [Class] [nvarchar](50) NOT NULL, [Contact] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ( [ID] ASC ) ) 3. 创建项目 现在,我们可以在Visual Studio中创建一个新的Windows Forms项目。可以选择C#或者VB.NET作为开发语言。 4. 添加数据源 在Visual Studio中,我们可以使用数据绑定来将数据与UI进行绑定。为了实现数据绑定,我们需要添加一个数据源。可以右击项目中的"Data Sources",选择"Add New Data Source"。在添加数据源的对话框中,选择"Database",然后选择我们刚刚创建的数据库。 5. 创建UI 现在,我们可以开始创建UI了。我们可以使用Visual Studio的"Windows Forms Designer"来创建UI。在UI中,我们可以添加一些文本框、按钮等控件。可以使用数据绑定来将这些控件与数据源进行绑定。 6. 编写代码 在UI中,我们需要编写一些代码实现具体的功能。比如,当用户点击"添加"按钮时,我们需要将用户输入的数据保存到数据库中。当用户点击"查询"按钮时,我们需要从数据库中查询数据并显示在UI中。 以下是一些示例代码: // 添加学生信息 private void btnAdd_Click(object sender, EventArgs e) { // 从文本框中获取用户输入的信息 string name = txtName.Text; string gender = rdoMale.Checked ? "男" : "女"; int age = int.Parse(txtAge.Text); string cls = txtClass.Text; string contact = txtContact.Text; // 将信息保存到数据库中 using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=StudentManagement;Integrated Security=True")) { conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO Student(Name, Gender, Age, Class, Contact) VALUES(@Name, @Gender, @Age, @Class, @Contact)", conn); cmd.Parameters.AddWithValue("@Name", name); cmd.Parameters.AddWithValue("@Gender", gender); cmd.Parameters.AddWithValue("@Age", age); cmd.Parameters.AddWithValue("@Class", cls); cmd.Parameters.AddWithValue("@Contact", contact); cmd.ExecuteNonQuery(); MessageBox.Show("添加成功!"); } } // 查询学生信息 private void btnQuery_Click(object sender, EventArgs e) { // 从数据库中查询信息 using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=StudentManagement;Integrated Security=True")) { conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Student", conn); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Student"); // 将查询结果显示在UI中 dgvStudent.DataSource = ds.Tables["Student"]; } } 7. 调试和部署 最后,我们可以在Visual Studio中进行调试,确保程序能够正常运行。如果一切正常,我们可以将程序打包成安装包,部署到目标机器上。 以上就是利用Visual Studio数据库制作系统的详细步骤和代码。由于篇幅限制,代码中可能存在一些细节问题,仅供参考。如果需要更详细的帮助,请提供更具体的问题和需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值