如何连接sql server [英] how to connect sql server

查看:64
本文介绍了如何连接sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从窗体应用程序提供输入并在我的sql中输出时。

如何连接我的sql server以用于ex< runat>

when i give input from window form application and get output in my sql.
how to connect my sql server for ex <runat>

推荐答案

首先,您需要一个连接字符串。

尝试使用Server Explorer窗格在VS中设置连接:

1)打开服务器资源管理器。 />
2)右键单击数据连接并选择添加连接

3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。

4)连接工作时,按确定

5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。



然后试试这个:

First, you need a connection string.
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Then try this:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable", con))
        {
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }



或者:


Or this:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }


string MyConnectionString =driver = {mysql odbc 3.51 driver}; Server = localhost; Database = Client_details; Uid = root; Pwd = ********;



OdbcConnection connection = new OdbcConnection(MyConnectionString);

OdbcCommand queryCmd;



试试

{

connection.Open();



queryCmd = connection.CreateCommand();

queryCmd.CommandText =Insert into table name()values();



var x = queryCmd.ExecuteNonQuery();

}

catch {}
string MyConnectionString = "driver={mysql odbc 3.51 driver};Server=localhost; Database = Client_details;Uid = root; Pwd = ********";

OdbcConnection connection = new OdbcConnection(MyConnectionString);
OdbcCommand queryCmd;

try
{
connection.Open();

queryCmd = connection.CreateCommand();
queryCmd.CommandText = "Insert Into table name()values()";

var x= queryCmd.ExecuteNonQuery();
}
catch{}


这篇关于如何连接sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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