如何使用SQL访问数据库 [英] How to Access a Database using SQL

查看:118
本文介绍了如何使用SQL访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是数据库访问的新手,所以请原谅我这些问题以及基本的问题.
我一直在关注一些示例,该示例如何使用sql命令访问数据库.
但是,我首先不太了解如何创建数据库"FreezerTemps",以及我需要在SqlConnection中添加哪些信息才能访问它.

我使用Microsoft Visual C#2010 Express创建数据库,该数据库位于代码本身的目录中.我相信Microsoft SQL Server 2008也能正常运行,但是在那里没有任何改变,因为我对这个概念很陌生.

我在下面附上我正在使用的代码.

感谢您阅读我的问题和代码.

亲切的问候,
Stelios.

Hello everybody, I am new to the database access so please forgive me for these question and if the seem basic.
I have been following some examples how to access a database using the sql commands.
However I do not quite understand firstly how to create a database "FreezerTemps", and what information I am required to add in the SqlConnection in order to get access to it.

I create the database using the Microsoft Visual C# 2010 Express, this is located in the directory of the code itself. I believe the Microsoft SQL Server 2008 is running as well but did not change anything there, as I am very new to this concept.

I attach the code I am using below.

Thank you for reading my question and code.

Kind regards,
Stelios.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WebDatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection cs = new SqlConnection("Data Source=nemesisfc; Initial Catalog=FreezerTemps; Integrated Security=TRUE");
            cs.Open();
            MessageBox.Show(cs.State.ToString());
            cs.Close();


         }
    }
}

推荐答案

SqlConnection cs = new SqlConnection("Data Source=servername; Initial Catalog=databasename; Integrated Security=TRUE");
            cs.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "write ur sql query here";
            cmd.ExecuteNonQuery();// for inser/delete/update
            cs.Close();
            //-------------------------------------------------

            SqlConnection cs1 = new SqlConnection("Data Source=nemesisfc; Initial Catalog=FreezerTemps; Integrated Security=TRUE");
            SqlCommand cmd1 = new SqlCommand();
            cs1.Open();
            cmd1.CommandText = "write ur sql select query here";
            DataSet dtst = new DataSet();
            SqlDataAdapter dp = new SqlDataAdapter(cmd1);
            dp.Fill(dtst);// for select query

            cs1.Close();



[edit]封装在预标签中的代码[/edit]



[edit]code wrapped in pre tags[/edit]


请按照以下步骤操作...
1.单击右侧的服务器资源管理器
2.右键单击数据库文件* .mdf,然后单击属性
3.*.mdf的IDE属性的右侧将可用复制连接字符串并将其粘贴到SQL Connection()上,如图所示

SqlConnection cs1 =新的SqlConnection(数据源=.\ SQLEXPRESS; AttachDbFilename =" C:\ Users \ bruda \ Documents \ Visual Studio 2008 \ WebSites \ sqldemo \ App_Data \ Database.mdf;集成安全性= True;用户实例= True );

这是您问题的解决方案不要忘了将其标记为答案如果可能,请为该答案评分

谢谢:-)
Follow the steps...
1.Click on Server Explorer on right side
2.Right click on you database file *.mdf and click on properties
3.Right side of your IDE properties of *.mdf will be available Copy the Connection string and paste it on your SQL Connection() as shown

SqlConnection cs1 = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\bruda\Documents\Visual Studio 2008\WebSites\sqldemo\App_Data\Database.mdf";Integrated Security=True;User Instance=True");

This is the solution for your question Do not forget to mark it as answer If possible rate this answer

Thank you :-)




我了解您对SQL Server本身以及如何在其中创建数据库有疑问.有关小型教程,请访问以下站点: http://www.quackit.com/sql_server/sql_server_2008/tutorial / [^ ]

要管理Sql Server,这是一个必须"下载: http://www.connectionstrings.com/ [ http://msdn.microsoft.com/zh-cn/library/kb9s9ks0.aspx [ ^ ]
Hi,

I understood that you have questions about SQL Server itself and how to create a database in it. For a small tutorial, have a look at this site: http://www.quackit.com/sql_server/sql_server_2008/tutorial/[^]

And to manage Sql Server this one is a ''must'' download: Microsoft® SQL Server® 2008 Management Studio Express [^]

As already suggested you can use also server explorer to see the connection string to use in the application. Also one good source for connection strings is: http://www.connectionstrings.com/[^]

And for using ADO.NET, have a look at these documents: http://msdn.microsoft.com/en-us/library/kb9s9ks0.aspx[^]


这篇关于如何使用SQL访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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