Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a project and i installed that setup on another computer,then i can't take backup of database

windows application in c#
visualstudio 2008

source code
----------------------------------------------------------------
C#
bool bBackUpStatus = true;
 
Cursor.Current = Cursors.WaitCursor;
 
if (Directory.Exists(@"c:\SQLBackup"))
{
if (File.Exists(@"c:\SQLBackup\wcBackUp1.bak"))
{
if (MessageBox.Show(@"Do you want to replace it?", "Back", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
File.Delete(@"c:\SQLBackup\wcBackUp1.bak");
}
else
bBackUpStatus = false;
}
}
else
Directory.CreateDirectory(@"c:\SQLBackup");

SqlConnection connect;
string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
connect = new SqlConnection(con);
connect.Open();
//----------------------------------------------------------------------------------------------------
 
//Execute SQL---------------
SqlCommand command;
command = new SqlCommand(@"backup database [C:\Users\Ravu\Desktop\Bill\Bill\bin\Debug\New.mdf] to disk ='c:\SQLBackup\New.bak' with init,stats=10", connect);
command.ExecuteNonQuery();
//-------------------------------------------------------------------------------------------------------------------
 
connect.Close();
 
MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);

Error details
--------------
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
C#
System.Data.SqlClient.SqlException: Could not locate entry in sysdatabases for database 'C:\Users\Ravu\Desktop\Bill\Bill\bin\Debug\New.mdf'. No entry found with that name. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Bill.MDIParent1.backUpToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Ravu\Desktop\Bill\Bill\MDIParent1.cs:line 299
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Posted
v4
Comments
ravuravu 8-Nov-12 2:24am    
hey please help me
ravuravu 8-Nov-12 2:25am    
please help me someone

1 solution

try this to take backup

C#
private void backup(string filename, string databasename)
{
conn.Open();
                string sqlQuery = "Backup database " + databasename+ " To Disk = '" + filename + "'";
                cmd = new SqlCommand(sqlQuery, conn);
                cmd.ExecuteNonQuery();
conn.Close();
}


backup("type_your_desired_filename.bak","type_your_database_name");
 
Share this answer
 
v3
Comments
ravuravu 8-Nov-12 3:46am    
hey how to give file name show an eg: pls
sahabiswarup 8-Nov-12 4:29am    
pass the file name as parameter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900