Tag: SQL服务器

如何备份或复制和还原或加载.Mdf附加的数据库文件

我使用SQL Server Express LocalDB,我有一个.mdf数据库文件免费浮动附加数据库。 我有一个带按钮的表单,按下按钮时会打开SaveFileDialog 。 我想将.mdf数据库保存在用户选择的任何地方,然后我有另一个表单,其中包含要恢复的按钮,或者在此.mdf数据库中复制并覆盖现有数据库。 private void button1_Click(object sender, EventArgs e) { ((( connectionString=”Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Expert-Pro-SoftDataBase.mdf;Integrated Security=True”))) this is the connection string saveFileDialog1.ShowDialog(); SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[“Expert”].ToString()); if (con.State.ToString() == “Closed”) { con.Open(); } SqlCommand com = new SqlCommand(“Backup Database @E TO DISK = @disk”, con); com.Parameters.Add(new SqlParameter(“@E”, “DatabaseTest.mdf”));(Another question is how can […]

如何在SQL Server中对两个值进行搜索查询

我制作了一个C#表单,在一个表中搜索两个值。 我的表称为具有字符串ID和字符串cust_name customers 。 我需要创建一个查找文本框的搜索查询文本可以在ID或cust_name ,所以我在textChanged发送此方法时进行了此SQL查询 search(txt_search.Text); SqlDataAdapter searchAdapter; private void search(string id) { searchAdapter = new SqlDataAdapter(@”Select * from Customers where cust_ID like ‘%’ ‘” + id + “‘ ‘%’ or cust_name like ‘%’ ‘” + id + “‘ ‘%'”, User.connection); } 请帮我把它弄好..

C#和SQL Server:插入/更新只在调试时才真正执行

我有一个相当简单的API用于添加,然后在SQL Server中更新一个小表。 当我在调试器中插入我的数据访问代码时,它将插入(或更新,表现出相同的奇怪行为),但是当我跳过相同的代码时则不会。 当然,我错过了一些明显的东西…… int result = 0; const string sql = “INSERT into MyProgress (ImportJobId, SiteCode, ProcessedAmount, TotalToProcess, ErrorCount, CreatedDateTime, LastModifiedDateTime) ” + “VALUES (@importJobId, @siteCode, @currentProgress, @totalPieces, 0, getdate(), getdate())”; using (var connection = GetSqlConnection()) using (var sqlCmd = GetSqlCommand(connection, sql)) { sqlCmd.Parameters.AddWithValue(“@importJobId”, importJobId); sqlCmd.Parameters.AddWithValue(“@siteCode”, siteCode); sqlCmd.Parameters.AddWithValue(“@currentProgress”, currentProgress); sqlCmd.Parameters.AddWithValue(“@totalPieces”, totalPieces); //SqlParameter outputParameter = […]

在c#中创建一个sql连接

我是这个网站的新手,也是编程的新手。 我目前正在通过销售点创建库存系统。 它使用模态和非模态forms。 我的问题是,我正在处理change password对话框,该对话框必须连接到数据库才能覆盖密码字段。 我使用的数据库是microsoft sql server management studio express。 以下是我到目前为止所提出的必要评论。 请注意,在“设计”表单中,我有一个与数据库绑定的combobox。 我哪里做错了? private void ChangePwdButton_Click(object sender, EventArgs e) { SqlConnection sqlconn = new SqlConnection(); sqlconn.ConnectionString = @”Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Gerald- dean Martin\Documents\SQL Server Management Studio Express\Projects\BodyMates.mdf;Integrated Security=True;User Instance=True”; sqlconn.Open(); string oldpwd = txtOldPwd.Text; string newpwd = txtNewPwd.Text; string confirmNewPwd = txtConfirmNewPwd.Text; string sqlquery = “UPDATE […]

带参数的SqlCommand

我使用以下代码从SQL Server表中进行选择: using (SqlConnection con = new SqlConnection(SqlConnectionString)) { string sql = @”SELECT * FROM movies WHERE title like ‘%’ + ‘” + searchQuery + “‘ + ‘%'”; using (var command = new SqlCommand(sql, con)) { con.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { …. } } } } 它工作得很好,但我想阻止SQL注入,所以我尝试使用: using (SqlConnection con […]

将多行逗号分隔的文本框值转换为单引号

我有一个代码,将列表框中的多个选定项目转换为一行逗号分隔值。 现在我想在多行文本框中实现相同的function,在多行文本框中,值将像a​​bc,def,pqr现在我想将它分开以便在数据库’abc’,’def’,’pqr’中进行搜索。 我正在使用的以下代码是为listbox如何修改它,以便我可以将其用于文本框目的 var SB = new StringBuilder(); foreach (ListItem lst in ListBox1.Items) { if (lst.Selected) { SB.Append(“‘” + lst.Value + “‘,”); } } var FinalString = SB.ToString().Substring(0, (SB.Length – 1));

将空值插入sql server中的int列

我有一个子程序将记录插入sql server表。 代码: public void Insert_Met(int Counts, char Gender) { Dictionary parameters = new Dictionary(); parameters.Add(“@Counts”, Counts); parameters.Add(“@Gender”, Gender); // run a stored procedure ExecuteNonQuery } 我的其他行代码, int counts = Convert.ToInt32(numberUpdowncontrol1.Text); // from a control, maybe empty then it is null. Insert_Met(counts,’M’); 我的问题是有时Counts可以为null,那么如何更改我的代码呢? 谢谢,

如何在不同条件下两次选择相同的字段,并将结果显示为单独的字段

我想在C#中创建一个表适配器,如下所示。 我希望在不同条件下两次使用相同的表格,并且必须显示为两个不同的字段。 这是我的源表: 在AttendanceReg表中,我需要根据AttendStatus Count每个学生的记录,以根据course and month查找他们Absent and present的天数。 示例查询: Select AttendanceReg.StudentID , AttendanceReg.Studname , AttendanceReg.StudSex , AttendanceReg.StudCourse , Count(AttendanceReg.AttendStatus) As Total_Present , DatePart(‘m’, AttendanceReg.DateOfAttendance) As Month From (AttendanceReg Inner Join LocalTable On AttendanceReg.StudCourse = LocalTable.AttendCourse And DatePart(‘M’, AttendanceReg.DateOfAttendance) = LocalTable.AttentMonth ) Where (AttendanceReg.AttendStatus = ‘Present’) Group By AttendanceReg.StudentID , AttendanceReg.Studname , AttendanceReg.StudSex , AttendanceReg.StudCourse […]

无法比较SqlBulkCopy中的列

这是我的代码: protected void Button1_Click(object sender, EventArgs e) { string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower(); string strFileName = FileUpload1.PostedFile.FileName.ToString(); FileUpload1.SaveAs(Server.MapPath(“~/Import/” + strFileName + strFileType)); string strNewPath = Server.MapPath(“~/Import/” + strFileName + strFileType); string excelConnectionString = String.Format(@”Provider=Microsoft.Jet.OLEDB.4.0;” + “Data Source=”+strNewPath +”; Extended Properties=Excel 8.0;”); //string excelConnectionString = String.Format(@”Provider=Microsoft.Jet.OLEDB.4.0;” + “Data Source=C:\\myFolder\\Book1.xls;” + “Extended Properties=Excel 8.0;”); // Create Connection to […]

SqlCommandBuilder()为基础表而不是视图创建插入/更新

我有两个模式,像这样: 模式’数据’ – >保存表,没有人可以从外部访问它们 Schema’ui’ – >包含可从外部访问的视图; 我们的想法是您可以在这些视图上选择/删除/更新/插入。 因此,我正在进行所有权链接。 例如: create table data.tblTest (TestKey int not null primary key); create view ui.vwTest as select * from data.tblTest; 现在,如果我以SQL Studio身份连接用户,一切正常: select * from ui.vwTest; — WORKS (this is correct) select * from data.tblTest; — ERROR (this is correct) insert into ui.vwTest (TestKey) values (17); — WORKS […]