如何与dabase和attachedbfilename建立连接 [英] how to make connection with the dabase, and the attachedbfilename

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

问题描述

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=|C:Users|blendy|Desktop|projekti|prova capston|capston2|capstone project|capstone project|loginforma.mdf; Integrated Security=True; User Instance=True");
        SqlCommand cmd;
        DataSet ds = new DataSet();





i有这段代码,

问题是这个:



ArgumentException未处理

无效值键'attachdbfilename'。



我如何连接数据库我有这个目录,我已经在那里显示。



i have this code,
the problem is this:

ArgumentException was unhandled
invalid value key 'attachdbfilename'.

how do i connect to database i have this directory that i have shown there.

推荐答案

见这里: http://www.connectionstrings.com/sqlconnection/using-an-user-instance-on -a-local-sql-server-express-instance / [ ^ ]

我认为 AttachDbFilename 值格式错误。如果仍然无法连接,可以尝试 SSMS [ ^ ]连接该数据库文件以查看如果有任何问题。



[更新]



see here: http://www.connectionstrings.com/sqlconnection/using-an-user-instance-on-a-local-sql-server-express-instance/[^]
I think AttachDbFilename value is in wrong format. If you still fail to connect, you can try SSMS [^] to connect that database file to see if anything is wrong with it.

[Update]

string connStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\blendy\Desktop\projekti\prova capston\capston2\capstone project\capstone project\loginforma.mdf; Integrated Security=True; User Instance=True";
SqlConnection con = new SqlConnection(connStr);





我认为上面的连接字符串可以正常工作。如果它不起作用,请使用SSMS将其附加到sql server实例。然后尝试这个连接字符串:



I think the above connection string will work. If it doesn't work, attach it to the sql server instance using SSMS. then try this connection string:

string connStr = @"Data Source=.\SQLEXPRESS; Initial Catalog=loginforma; Integrated Security=True; User Instance=True";


查看是否有日志文件并删除它(如果有)(loginforma.ldf)。
See if there's a log file in there and delete it if there is (loginforma.ldf).


这是代码,问题是:connectionstring属性尚未初始化



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;

使用System.Data.SqlClient;



公共部分课程表格1:表格

{

public Form1()

{

InitializeComponent();

}

string connStr = @Data Source = .\SQLEXPRESS; AttachDbFilename = C:\ Users\blendy \Desktop\projekti\prova capston\capston2\capstone project\capstone project\Baza1.mdf;综合安全=真; User Instance = True;

SqlConnection con = new SqlConnection();

SqlCommand cmd;

DataSet ds = new DataSet(); < br $> b $ b





private void btnlogin_Click(object sender,EventArgs e)

{

尝试

{

if(txtEmri.Text ==|| txtFjalkalimi.Text ==)

{

MessageBox.Show(Shkruaj Emrin dhe Fjalkalimin。);

返回;

}



cmd =新的SqlCommand(SELECT * FROM tbl_Regjistrimi,其中Emri ='+ txtEmri.Text +'和Fjalëkalimi='+ txtFjalkalimi.Text +',con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);

int i = ds.Tables [0] .Rows .Count;

if(i == 1)

{

connStr =Mirëseerdhe+ txtEmri.Text;

this.Hide();

Form3 f3 = new Form3(connStr);

f3.Show();

ds.Clear();

}

其他

{

MessageBox.Show(Nukjenitëregjistruarose gabimkenishëenuarEmrin/ Fjalëkalimin);

txtFjalkalimi.Text =;

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}
this is the code, problem is: the connectionstring property has not been initializied

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;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string connStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\blendy\Desktop\projekti\prova capston\capston2\capstone project\capstone project\Baza1.mdf; Integrated Security=True; User Instance=True";
SqlConnection con = new SqlConnection();
SqlCommand cmd;
DataSet ds = new DataSet();



private void btnlogin_Click(object sender, EventArgs e)
{
try
{
if (txtEmri.Text == "" || txtFjalkalimi.Text == "")
{
MessageBox.Show(" Shkruaj Emrin dhe Fjalkalimin.");
return;
}

cmd = new SqlCommand("SELECT * FROM tbl_Regjistrimi where Emri='" + txtEmri.Text + "' and Fjalëkalimi='" + txtFjalkalimi.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i == 1)
{
connStr = "Mirë se erdhe " + txtEmri.Text;
this.Hide();
Form3 f3 = new Form3(connStr);
f3.Show();
ds.Clear();
}
else
{
MessageBox.Show("Nuk jeni të regjistruar ose gabim keni shëenuar Emrin/Fjalëkalimin");
txtFjalkalimi.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


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

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